home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / resources.pak / Unnamed File 000120.txt < prev    next >
Text File  |  2013-04-03  |  228KB  |  5,807 lines

  1. /*
  2.  * Copyright (C) 2012 Google Inc. All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are
  6.  * met:
  7.  *
  8.  *     * Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above
  11.  * copyright notice, this list of conditions and the following disclaimer
  12.  * in the documentation and/or other materials provided with the
  13.  * distribution.
  14.  *     * Neither the name of Google Inc. nor the names of its
  15.  * contributors may be used to endorse or promote products derived from
  16.  * this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. // CodeMirror is the only global var we claim
  32. window.CodeMirror = (function() {
  33.   "use strict";
  34.  
  35.   // BROWSER SNIFFING
  36.  
  37.   // Crude, but necessary to handle a number of hard-to-feature-detect
  38.   // bugs and behavior differences.
  39.   var gecko = /gecko\/\d{7}/i.test(navigator.userAgent);
  40.   var ie = /MSIE \d/.test(navigator.userAgent);
  41.   var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent);
  42.   var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent);
  43.   var ie_lt10 = /MSIE [1-9]\b/.test(navigator.userAgent);
  44.   var webkit = /WebKit\//.test(navigator.userAgent);
  45.   var chrome = /Chrome\//.test(navigator.userAgent);
  46.   var opera = /Opera\//.test(navigator.userAgent);
  47.   var safari = /Apple Computer/.test(navigator.vendor);
  48.   var khtml = /KHTML\//.test(navigator.userAgent);
  49.   var mac_geLion = /Mac OS X 1\d\D([7-9]|\d\d)\D/.test(navigator.userAgent);
  50.   var mac_geMountainLion = /Mac OS X 1\d\D([8-9]|\d\d)\D/.test(navigator.userAgent);
  51.   var phantom = /PhantomJS/.test(navigator.userAgent);
  52.  
  53.   var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent);
  54.   var mac = ios || /Mac/.test(navigator.platform);
  55.   var win = /Win/.test(navigator.platform);
  56.  
  57.   // Optimize some code when these features are not used
  58.   var sawReadOnlySpans = false, sawCollapsedSpans = false;
  59.  
  60.   // CONSTRUCTOR
  61.  
  62.   function CodeMirror(place, options) {
  63.     if (!(this instanceof CodeMirror)) return new CodeMirror(place, options);
  64.     
  65.     this.options = options = options || {};
  66.     // Determine effective options based on given values and defaults.
  67.     for (var opt in defaults) if (!options.hasOwnProperty(opt) && defaults.hasOwnProperty(opt))
  68.       options[opt] = defaults[opt];
  69.     setGuttersForLineNumbers(options);
  70.  
  71.     var display = this.display = makeDisplay(place);
  72.     display.wrapper.CodeMirror = this;
  73.     updateGutters(this);
  74.     if (options.autofocus) focusInput(this);
  75.  
  76.     var doc = new BranchChunk([new LeafChunk([makeLine("", null, textHeight(display))])]);
  77.     // The selection. These are always maintained to point at valid
  78.     // positions. Inverted is used to remember that the user is
  79.     // selecting bottom-to-top.
  80.     this.view = {
  81.       doc: doc,
  82.       // frontier is the point up to which the content has been parsed,
  83.       frontier: 0, highlight: new Delayed(),
  84.       sel: {from: {line: 0, ch: 0}, to: {line: 0, ch: 0}, inverted: false, shift: false},
  85.       scrollTop: 0, scrollLeft: 0,
  86.       overwrite: false, focused: false,
  87.       // Tracks the maximum line length so that
  88.       // the horizontal scrollbar can be kept
  89.       // static when scrolling.
  90.       maxLine: getLine(doc, 0),
  91.       maxLineLength: 0,
  92.       maxLineChanged: false,
  93.       suppressEdits: false,
  94.       goalColumn: null,
  95.       cantEdit: false
  96.     };
  97.     loadMode(this);
  98.  
  99.     // Initialize the content.
  100.     this.setValue(options.value || "");
  101.     // Override magic textarea content restore that IE sometimes does
  102.     // on our hidden textarea on reload
  103.     if (ie) setTimeout(bind(resetInput, this, true), 20);
  104.     this.view.history = makeHistory();
  105.  
  106.     registerEventHandlers(this);
  107.     // IE throws unspecified error in certain cases, when
  108.     // trying to access activeElement before onload
  109.     var hasFocus; try { hasFocus = (document.activeElement == display.input); } catch(e) { }
  110.     if (hasFocus || options.autofocus) setTimeout(bind(onFocus, this), 20);
  111.     else onBlur(this);
  112.  
  113.     operation(this, function() {
  114.       for (var opt in optionHandlers)
  115.         if (optionHandlers.propertyIsEnumerable(opt))
  116.           optionHandlers[opt](this, options[opt], Init);
  117.       for (var i = 0; i < initHooks.length; ++i) initHooks[i](this);
  118.     })();
  119.   }
  120.  
  121.   // DISPLAY CONSTRUCTOR
  122.  
  123.   function makeDisplay(place) {
  124.     var d = {};
  125.     var input = d.input = elt("textarea", null, null, "position: absolute; padding: 0; width: 1px; height: 1em; outline: none;");
  126.     input.setAttribute("wrap", "off"); input.setAttribute("autocorrect", "off"); input.setAttribute("autocapitalize", "off");
  127.     // Wraps and hides input textarea
  128.     d.inputDiv = elt("div", [input], null, "overflow: hidden; position: relative; width: 3px; height: 0px;");
  129.     // The actual fake scrollbars.
  130.     d.scrollbarH = elt("div", [elt("div", null, null, "height: 1px")], "CodeMirror-hscrollbar");
  131.     d.scrollbarV = elt("div", [elt("div", null, null, "width: 1px")], "CodeMirror-vscrollbar");
  132.     d.scrollbarFiller = elt("div", null, "CodeMirror-scrollbar-filler");
  133.     // DIVs containing the selection and the actual code
  134.     d.lineDiv = elt("div");
  135.     d.selectionDiv = elt("div", null, null, "position: relative; z-index: 1");
  136.     // Blinky cursor, and element used to ensure cursor fits at the end of a line
  137.     d.cursor = elt("pre", "\u00a0", "CodeMirror-cursor");
  138.     // Secondary cursor, shown when on a 'jump' in bi-directional text
  139.     d.otherCursor = elt("pre", "\u00a0", "CodeMirror-cursor CodeMirror-secondarycursor");
  140.     // Used to measure text size
  141.     d.measure = elt("div", null, "CodeMirror-measure");
  142.     // Wraps everything that needs to exist inside the vertically-padded coordinate system
  143.     d.lineSpace = elt("div", [d.measure, d.selectionDiv, d.lineDiv, d.cursor, d.otherCursor],
  144.                          null, "position: relative; outline: none");
  145.     // Moved around its parent to cover visible view
  146.     d.mover = elt("div", [elt("div", [d.lineSpace], "CodeMirror-lines")], null, "position: relative");
  147.     d.gutters = elt("div", null, "CodeMirror-gutters");
  148.     d.lineGutter = null;
  149.     // Set to the height of the text, causes scrolling
  150.     d.sizer = elt("div", [d.mover], "CodeMirror-sizer");
  151.     // D is needed because behavior of elts with overflow: auto and padding is inconsistent across browsers
  152.     d.heightForcer = elt("div", "\u00a0", null, "position: absolute; height: " + scrollerCutOff + "px");
  153.     // Provides scrolling
  154.     d.scroller = elt("div", [d.sizer, d.heightForcer], "CodeMirror-scroll");
  155.     d.scroller.setAttribute("tabIndex", "-1");
  156.     // The element in which the editor lives.
  157.     d.wrapper = elt("div", [d.gutters, d.inputDiv, d.scrollbarH, d.scrollbarV,
  158.                             d.scrollbarFiller, d.scroller], "CodeMirror");
  159.     // Work around IE7 z-index bug
  160.     if (ie_lt8) { d.gutters.style.zIndex = -1; d.scroller.style.paddingRight = 0; }
  161.     if (place.appendChild) place.appendChild(d.wrapper); else place(d.wrapper);
  162.  
  163.     // Needed to hide big blue blinking cursor on Mobile Safari
  164.     if (ios) input.style.width = "0px";
  165.     if (!webkit) d.scroller.draggable = true;
  166.     // Needed to handle Tab key in KHTML
  167.     if (khtml) { d.inputDiv.style.height = "1px"; d.inputDiv.style.position = "absolute"; }
  168.     // Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8).
  169.     else if (ie_lt8) d.scrollbarH.style.minWidth = d.scrollbarV.style.minWidth = "18px";
  170.  
  171.     // Current visible range (may be bigger than the view window).
  172.     d.viewOffset = d.showingFrom = d.showingTo = d.lastSizeC = 0;
  173.  
  174.     // Used to only resize the line number gutter when necessary (when
  175.     // the amount of lines crosses a boundary that makes its width change)
  176.     d.lineNumWidth = d.lineNumInnerWidth = d.lineNumChars = null;
  177.     // See readInput and resetInput
  178.     d.prevInput = "";
  179.     // Set to true when a non-horizontal-scrolling widget is added. As
  180.     // an optimization, widget aligning is skipped when d is false.
  181.     d.alignWidgets = false;
  182.     // Flag that indicates whether we currently expect input to appear
  183.     // (after some event like 'keypress' or 'input') and are polling
  184.     // intensively.
  185.     d.pollingFast = false;
  186.     // Self-resetting timeout for the poller
  187.     d.poll = new Delayed();
  188.     // True when a drag from the editor is active
  189.     d.draggingText = false;
  190.  
  191.     d.cachedCharWidth = d.cachedTextHeight = null;
  192.     d.measureLineCache = [];
  193.     d.measureLineCache.pos = 0;
  194.  
  195.     // Tracks when resetInput has punted to just putting a short
  196.     // string instead of the (large) selection.
  197.     d.inaccurateSelection = false;
  198.  
  199.     // Used to adjust overwrite behaviour when a paste has been
  200.     // detected
  201.     d.pasteIncoming = false;
  202.  
  203.     return d;
  204.   }
  205.  
  206.   // STATE UPDATES
  207.  
  208.   // Used to get the editor into a consistent state again when options change.
  209.  
  210.   function loadMode(cm) {
  211.     var doc = cm.view.doc;
  212.     cm.view.mode = CodeMirror.getMode(cm.options, cm.options.mode);
  213.     doc.iter(0, doc.size, function(line) { line.stateAfter = null; });
  214.     cm.view.frontier = 0;
  215.     startWorker(cm, 100);
  216.   }
  217.  
  218.   function wrappingChanged(cm) {
  219.     var doc = cm.view.doc, th = textHeight(cm.display);
  220.     if (cm.options.lineWrapping) {
  221.       cm.display.wrapper.className += " CodeMirror-wrap";
  222.       var perLine = cm.display.scroller.clientWidth / charWidth(cm.display) - 3;
  223.       doc.iter(0, doc.size, function(line) {
  224.         if (line.height == 0) return;
  225.         var guess = Math.ceil(line.text.length / perLine) || 1;
  226.         if (guess != 1) updateLineHeight(line, guess * th);
  227.       });
  228.       cm.display.sizer.style.minWidth = "";
  229.     } else {
  230.       cm.display.wrapper.className = cm.display.wrapper.className.replace(" CodeMirror-wrap", "");
  231.       computeMaxLength(cm.view);
  232.       doc.iter(0, doc.size, function(line) {
  233.         if (line.height != 0) updateLineHeight(line, th);
  234.       });
  235.     }
  236.     regChange(cm, 0, doc.size);
  237.   }
  238.  
  239.   function keyMapChanged(cm) {
  240.     var style = keyMap[cm.options.keyMap].style;
  241.     cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-keymap-\S+/g, "") +
  242.       (style ? " cm-keymap-" + style : "");
  243.   }
  244.  
  245.   function themeChanged(cm) {
  246.     cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-s-\S+/g, "") +
  247.       cm.options.theme.replace(/(^|\s)\s*/g, " cm-s-");
  248.   }
  249.  
  250.   function guttersChanged(cm) {
  251.     updateGutters(cm);
  252.     updateDisplay(cm, true);
  253.   }
  254.  
  255.   function updateGutters(cm) {
  256.     var gutters = cm.display.gutters, specs = cm.options.gutters;
  257.     removeChildren(gutters);
  258.     for (var i = 0; i < specs.length; ++i) {
  259.       var gutterClass = specs[i];
  260.       var gElt = gutters.appendChild(elt("div", null, "CodeMirror-gutter " + gutterClass));
  261.       if (gutterClass == "CodeMirror-linenumbers") {
  262.         cm.display.lineGutter = gElt;
  263.         gElt.style.width = (cm.display.lineNumWidth || 1) + "px";
  264.       }
  265.     }
  266.     gutters.style.display = i ? "" : "none";
  267.   }
  268.  
  269.   function lineLength(doc, line) {
  270.     if (line.height == 0) return 0;
  271.     var len = line.text.length, merged, cur = line;
  272.     while (merged = collapsedSpanAtStart(cur)) {
  273.       var found = merged.find();
  274.       cur = getLine(doc, found.from.line);
  275.       len += found.from.ch - found.to.ch;
  276.     }
  277.     cur = line;
  278.     while (merged = collapsedSpanAtEnd(cur)) {
  279.       var found = merged.find();
  280.       len -= cur.text.length - found.from.ch;
  281.       cur = getLine(doc, found.to.line);
  282.       len += cur.text.length - found.to.ch;
  283.     }
  284.     return len;
  285.   }
  286.  
  287.   function computeMaxLength(view) {
  288.     view.maxLine = getLine(view.doc, 0);
  289.     view.maxLineLength = lineLength(view.doc, view.maxLine);
  290.     view.maxLineChanged = true;
  291.     view.doc.iter(1, view.doc.size, function(line) {
  292.       var len = lineLength(view.doc, line);
  293.       if (len > view.maxLineLength) {
  294.         view.maxLineLength = len;
  295.         view.maxLine = line;
  296.       }
  297.     });
  298.   }
  299.  
  300.   // Make sure the gutters options contains the element
  301.   // "CodeMirror-linenumbers" when the lineNumbers option is true.
  302.   function setGuttersForLineNumbers(options) {
  303.     var found = false;
  304.     for (var i = 0; i < options.gutters.length; ++i) {
  305.       if (options.gutters[i] == "CodeMirror-linenumbers") {
  306.         if (options.lineNumbers) found = true;
  307.         else options.gutters.splice(i--, 1);
  308.       }
  309.     }
  310.     if (!found && options.lineNumbers)
  311.       options.gutters.push("CodeMirror-linenumbers");
  312.   }
  313.  
  314.   // SCROLLBARS
  315.  
  316.   // Re-synchronize the fake scrollbars with the actual size of the
  317.   // content. Optionally force a scrollTop.
  318.   function updateScrollbars(d /* display */, docHeight, scrollTop) {
  319.     d.sizer.style.minHeight = d.heightForcer.style.top = (docHeight + 2 * paddingTop(d)) + "px";
  320.     var needsH = d.scroller.scrollWidth > d.scroller.clientWidth;
  321.     var needsV = d.scroller.scrollHeight > d.scroller.clientHeight;
  322.     if (needsV) {
  323.       d.scrollbarV.style.display = "block";
  324.       d.scrollbarV.style.bottom = needsH ? scrollbarWidth(d.measure) + "px" : "0";
  325.       d.scrollbarV.firstChild.style.height = 
  326.         (d.scroller.scrollHeight - d.scroller.clientHeight + d.scrollbarV.clientHeight) + "px";
  327.       if (scrollTop != null) {
  328.         d.scrollbarV.scrollTop = d.scroller.scrollTop = scrollTop;
  329.         // 'Nudge' the scrollbar to work around a Webkit bug where,
  330.         // in some situations, we'd end up with a scrollbar that
  331.         // reported its scrollTop (and looked) as expected, but
  332.         // *behaved* as if it was still in a previous state (i.e.
  333.         // couldn't scroll up, even though it appeared to be at the
  334.         // bottom).
  335.         if (webkit) setTimeout(function() {
  336.           if (d.scrollbarV.scrollTop != scrollTop) return;
  337.           d.scrollbarV.scrollTop = scrollTop + (scrollTop ? -1 : 1);
  338.           d.scrollbarV.scrollTop = scrollTop;
  339.         }, 0);
  340.       }
  341.     } else d.scrollbarV.style.display = "";
  342.     if (needsH) {
  343.       d.scrollbarH.style.display = "block";
  344.       d.scrollbarH.style.right = needsV ? scrollbarWidth(d.measure) + "px" : "0";
  345.       d.scrollbarH.firstChild.style.width =
  346.         (d.scroller.scrollWidth - d.scroller.clientWidth + d.scrollbarH.clientWidth) + "px";
  347.     } else d.scrollbarH.style.display = "";
  348.     if (needsH && needsV) {
  349.       d.scrollbarFiller.style.display = "block";
  350.       d.scrollbarFiller.style.height = d.scrollbarFiller.style.width = scrollbarWidth(d.measure) + "px";
  351.     } else d.scrollbarFiller.style.display = "";
  352.  
  353.     if (mac_geLion && scrollbarWidth(d.measure) === 0)
  354.       d.scrollbarV.style.minWidth = d.scrollbarH.style.minHeight = mac_geMountainLion ? "18px" : "12px";
  355.   }
  356.  
  357.   function visibleLines(display, doc, viewPort) {
  358.     var top = display.scroller.scrollTop, height = display.wrapper.clientHeight;
  359.     if (typeof viewPort == "number") top = viewPort;
  360.     else if (viewPort) {top = viewPort.top; height = viewPort.bottom - viewPort.top;}
  361.     top = Math.floor(top - paddingTop(display));
  362.     var bottom = Math.ceil(top + height);
  363.     return {from: lineAtHeight(doc, top), to: lineAtHeight(doc, bottom)};
  364.   }
  365.  
  366.   // LINE NUMBERS
  367.  
  368.   function alignVertically(display) {
  369.     if (!display.alignWidgets && !display.gutters.firstChild) return;
  370.     var l = compensateForHScroll(display) + "px";
  371.     for (var n = display.lineDiv.firstChild; n; n = n.nextSibling) if (n.alignable) {
  372.       for (var i = 0, a = n.alignable; i < a.length; ++i) a[i].style.left = l;
  373.     }
  374.   }
  375.  
  376.   function maybeUpdateLineNumberWidth(cm) {
  377.     if (!cm.options.lineNumbers) return false;
  378.     var doc = cm.view.doc, last = lineNumberFor(cm.options, doc.size - 1), display = cm.display;
  379.     if (last.length != display.lineNumChars) {
  380.       var test = display.measure.appendChild(elt("div", [elt("div", last)],
  381.                                                  "CodeMirror-linenumber CodeMirror-gutter-elt"));
  382.       var innerW = test.firstChild.offsetWidth, padding = test.offsetWidth - innerW;
  383.       display.lineGutter.style.width = "";
  384.       display.lineNumInnerWidth = Math.max(innerW, display.lineGutter.offsetWidth - padding);
  385.       display.lineNumWidth = display.lineNumInnerWidth + padding;
  386.       display.lineNumChars = display.lineNumInnerWidth ? last.length : -1;
  387.       display.lineGutter.style.width = display.lineNumWidth + "px";
  388.       return true;
  389.     }
  390.     return false;
  391.   }
  392.  
  393.   function lineNumberFor(options, i) {
  394.     return String(options.lineNumberFormatter(i + options.firstLineNumber));
  395.   }
  396.   function compensateForHScroll(display) {
  397.     return display.scroller.getBoundingClientRect().left - display.sizer.getBoundingClientRect().left;
  398.   }
  399.  
  400.   // DISPLAY DRAWING
  401.  
  402.   function updateDisplay(cm, changes, viewPort) {
  403.     var oldFrom = cm.display.showingFrom, oldTo = cm.display.showingTo;
  404.     var updated = updateDisplayInner(cm, changes, viewPort);
  405.     if (updated) {
  406.       signalLater(cm, cm, "update", cm);
  407.       if (cm.display.showingFrom != oldFrom || cm.display.showingTo != oldTo)
  408.         signalLater(cm, cm, "viewportChange", cm, cm.display.showingFrom, cm.display.showingTo);
  409.     }
  410.     updateSelection(cm);
  411.     updateScrollbars(cm.display, cm.view.doc.height, typeof viewPort == "number" ? viewPort : null);
  412.     return updated;
  413.   }
  414.  
  415.   // Uses a set of changes plus the current scroll position to
  416.   // determine which DOM updates have to be made, and makes the
  417.   // updates.
  418.   function updateDisplayInner(cm, changes, viewPort) {
  419.     var display = cm.display, doc = cm.view.doc;
  420.     if (!display.wrapper.clientWidth) {
  421.       display.showingFrom = display.showingTo = display.viewOffset = 0;
  422.       return;
  423.     }
  424.  
  425.     // Compute the new visible window
  426.     // If scrollTop is specified, use that to determine which lines
  427.     // to render instead of the current scrollbar position.
  428.     var visible = visibleLines(display, doc, viewPort);
  429.     // Bail out if the visible area is already rendered and nothing changed.
  430.     if (changes !== true && changes.length == 0 &&
  431.         visible.from > display.showingFrom && visible.to < display.showingTo)
  432.       return;
  433.  
  434.     if (changes && maybeUpdateLineNumberWidth(cm))
  435.       changes = true;
  436.     display.sizer.style.marginLeft = display.scrollbarH.style.left = display.gutters.offsetWidth + "px";
  437.  
  438.     // When merged lines are present, the line that needs to be
  439.     // redrawn might not be the one that was changed.
  440.     if (changes !== true && sawCollapsedSpans)
  441.       for (var i = 0; i < changes.length; ++i) {
  442.         var ch = changes[i], merged;
  443.         while (merged = collapsedSpanAtStart(getLine(doc, ch.from))) {
  444.           var from = merged.find().from.line;
  445.           if (ch.diff) ch.diff -= ch.from - from;
  446.           ch.from = from;
  447.         }
  448.       }
  449.  
  450.     // Used to determine which lines need their line numbers updated
  451.     var positionsChangedFrom = changes === true ? 0 : Infinity;
  452.     if (cm.options.lineNumbers && changes && changes !== true)
  453.       for (var i = 0; i < changes.length; ++i)
  454.         if (changes[i].diff) { positionsChangedFrom = changes[i].from; break; }
  455.  
  456.     var from = Math.max(visible.from - cm.options.viewportMargin, 0);
  457.     var to = Math.min(doc.size, visible.to + cm.options.viewportMargin);
  458.     if (display.showingFrom < from && from - display.showingFrom < 20) from = display.showingFrom;
  459.     if (display.showingTo > to && display.showingTo - to < 20) to = Math.min(doc.size, display.showingTo);
  460.  
  461.     // Create a range of theoretically intact lines, and punch holes
  462.     // in that using the change info.
  463.     var intact = changes === true ? [] :
  464.       computeIntact([{from: display.showingFrom, to: display.showingTo, domStart: 0}], changes);
  465.     // Clip off the parts that won't be visible
  466.     var intactLines = 0;
  467.     for (var i = 0; i < intact.length; ++i) {
  468.       var range = intact[i];
  469.       if (range.from < from) {range.domStart += (from - range.from); range.from = from;}
  470.       if (range.to > to) range.to = to;
  471.       if (range.from >= range.to) intact.splice(i--, 1);
  472.       else intactLines += range.to - range.from;
  473.     }
  474.     if (intactLines == to - from && from == display.showingFrom && to == display.showingTo)
  475.       return;
  476.     intact.sort(function(a, b) {return a.domStart - b.domStart;});
  477.  
  478.     if (intactLines < (to - from) * .7) display.lineDiv.style.display = "none";
  479.     patchDisplay(cm, from, to, intact, positionsChangedFrom);
  480.     display.lineDiv.style.display = "";
  481.  
  482.     var different = from != display.showingFrom || to != display.showingTo ||
  483.       display.lastSizeC != display.wrapper.clientHeight;
  484.     // This is just a bogus formula that detects when the editor is
  485.     // resized or the font size changes.
  486.     if (different) display.lastSizeC = display.wrapper.clientHeight;
  487.     display.showingFrom = from; display.showingTo = to;
  488.     display.viewOffset = heightAtLine(cm, getLine(doc, from));
  489.     startWorker(cm, 100);
  490.  
  491.     // Since this is all rather error prone, it is honoured with the
  492.     // only assertion in the whole file.
  493.     if (display.lineDiv.childNodes.length != display.showingTo - display.showingFrom)
  494.       throw new Error("BAD PATCH! " + JSON.stringify(intact) + " size=" + (display.showingTo - display.showingFrom) +
  495.                       " nodes=" + display.lineDiv.childNodes.length);
  496.  
  497.     // Update line heights for visible lines based on actual DOM
  498.     // sizes
  499.     var curNode = display.lineDiv.firstChild, relativeTo = curNode.offsetTop;
  500.     doc.iter(display.showingFrom, display.showingTo, function(line) {
  501.       // Work around bizarro IE7 bug where, sometimes, our curNode
  502.       // is magically replaced with a new node in the DOM, leaving
  503.       // us with a reference to an orphan (nextSibling-less) node.
  504.       if (!curNode) return;
  505.       if (!lineIsHidden(line)) {
  506.         var end = curNode.offsetHeight + curNode.offsetTop;
  507.         var height = end - relativeTo, diff = line.height - height;
  508.         if (height < 2) height = textHeight(display);
  509.         relativeTo = end;
  510.         if (diff > .001 || diff < -.001)
  511.           updateLineHeight(line, height);
  512.       }
  513.       curNode = curNode.nextSibling;
  514.     });
  515.  
  516.     // Position the mover div to align with the current virtual scroll position
  517.     display.mover.style.top = display.viewOffset + "px";
  518.     return true;
  519.   }
  520.  
  521.   function computeIntact(intact, changes) {
  522.     for (var i = 0, l = changes.length || 0; i < l; ++i) {
  523.       var change = changes[i], intact2 = [], diff = change.diff || 0;
  524.       for (var j = 0, l2 = intact.length; j < l2; ++j) {
  525.         var range = intact[j];
  526.         if (change.to <= range.from && change.diff)
  527.           intact2.push({from: range.from + diff, to: range.to + diff,
  528.                         domStart: range.domStart});
  529.         else if (change.to <= range.from || change.from >= range.to)
  530.           intact2.push(range);
  531.         else {
  532.           if (change.from > range.from)
  533.             intact2.push({from: range.from, to: change.from, domStart: range.domStart});
  534.           if (change.to < range.to)
  535.             intact2.push({from: change.to + diff, to: range.to + diff,
  536.                           domStart: range.domStart + (change.to - range.from)});
  537.         }
  538.       }
  539.       intact = intact2;
  540.     }
  541.     return intact;
  542.   }
  543.  
  544.   function getDimensions(cm) {
  545.     var d = cm.display, left = {}, width = {};
  546.     for (var n = d.gutters.firstChild, i = 0; n; n = n.nextSibling, ++i) {
  547.       left[cm.options.gutters[i]] = n.offsetLeft;
  548.       width[cm.options.gutters[i]] = n.offsetWidth;
  549.     }
  550.     return {fixedPos: compensateForHScroll(d),
  551.             gutterTotalWidth: d.gutters.offsetWidth,
  552.             gutterLeft: left,
  553.             gutterWidth: width,
  554.             wrapperWidth: d.wrapper.clientWidth};
  555.   }
  556.  
  557.   function patchDisplay(cm, from, to, intact, updateNumbersFrom) {
  558.     function killNode(node) {
  559.       var tmp = node.nextSibling;
  560.       node.parentNode.removeChild(node);
  561.       return tmp;
  562.     }
  563.     var dims = getDimensions(cm);
  564.     var display = cm.display, lineNumbers = cm.options.lineNumbers;
  565.     // The first pass removes the DOM nodes that aren't intact.
  566.     if (!intact.length) {
  567.       // old IE does bad things to nodes when .innerHTML = "" is used on a parent
  568.       // we still need widgets and markers intact to add back to the new content later
  569.       if (ie_lt10) for (var ld = display.lineDiv, tmp = ld.firstChild; tmp; tmp = ld.firstChild) ld.removeChild(tmp);
  570.       else removeChildren(display.lineDiv);
  571.     } else {
  572.       var domPos = 0, curNode = display.lineDiv.firstChild, n;
  573.       for (var i = 0; i < intact.length; ++i) {
  574.         var cur = intact[i];
  575.         while (cur.domStart > domPos) {curNode = killNode(curNode); domPos++;}
  576.         for (var j = cur.from, e = cur.to; j < e; ++j) {
  577.           if (lineNumbers && updateNumbersFrom <= j && curNode.firstChild)
  578.             setTextContent(curNode.firstChild.firstChild, lineNumberFor(cm.options, j));
  579.           curNode = curNode.nextSibling; domPos++;
  580.         }
  581.       }
  582.       while (curNode) curNode = killNode(curNode);
  583.     }
  584.     // This pass fills in the lines that actually changed.
  585.     var nextIntact = intact.shift(), curNode = display.lineDiv.firstChild;
  586.     var j = from;
  587.  
  588.     cm.view.doc.iter(from, to, function(line) {
  589.       if (nextIntact && nextIntact.to == j) nextIntact = intact.shift();
  590.       if (!nextIntact || nextIntact.from > j)
  591.         display.lineDiv.insertBefore(buildLineElement(cm, line, j, dims), curNode);
  592.       else
  593.         curNode = curNode.nextSibling;
  594.       ++j;
  595.     });
  596.   }
  597.  
  598.   function buildLineElement(cm, line, lineNo, dims) {
  599.     if (line.height == 0) return elt("div");
  600.  
  601.     var lineElement = line.height == 0 ? elt("div") : lineContent(cm, line);
  602.     var markers = line.gutterMarkers, display = cm.display;
  603.  
  604.     if (!cm.options.lineNumbers && !markers && !line.bgClass && !line.wrapClass &&
  605.         (!line.widgets || !line.widgets.length)) return lineElement;
  606.  
  607.     // Lines with gutter elements or a background class need
  608.     // to be wrapped again, and have the extra elements added
  609.     // to the wrapper div
  610.  
  611.     var wrap = elt("div", null, line.wrapClass, "position: relative");
  612.     if (cm.options.lineNumbers || markers) {
  613.       var gutterWrap = wrap.appendChild(elt("div", null, null, "position: absolute; left: " +
  614.                                             dims.fixedPos + "px"));
  615.       wrap.alignable = [gutterWrap];
  616.       if (cm.options.lineNumbers)
  617.         gutterWrap.appendChild(elt("div", lineNumberFor(cm.options, lineNo),
  618.                                    "CodeMirror-linenumber CodeMirror-gutter-elt",
  619.                                    "left: " + dims.gutterLeft["CodeMirror-linenumbers"] + "px; width: "
  620.                                    + display.lineNumInnerWidth + "px"));
  621.       if (markers)
  622.         for (var k = 0; k < cm.options.gutters.length; ++k) {
  623.           var id = cm.options.gutters[k], found = markers.hasOwnProperty(id) && markers[id];
  624.           if (found) {
  625.             gutterWrap.appendChild(elt("div", [found], "CodeMirror-gutter-elt", "left: " +
  626.                                        dims.gutterLeft[id] + "px; width: " + dims.gutterWidth[id] + "px"));
  627.           }
  628.         }
  629.     }
  630.     // Kludge to make sure the styled element lies behind the selection (by z-index)
  631.     if (line.bgClass)
  632.       wrap.appendChild(elt("div", "\u00a0", line.bgClass + " CodeMirror-linebackground"));
  633.     wrap.appendChild(lineElement);
  634.     if (line.widgets)
  635.       for (var i = 0, ws = line.widgets; i < ws.length; ++i) {
  636.         var widget = ws[i], node = elt("div", [widget.node], "CodeMirror-linewidget");
  637.         node.widget = widget;
  638.         if (widget.noHScroll) {
  639.           (wrap.alignable || (wrap.alignable = [])).push(node);
  640.           var width = dims.wrapperWidth;
  641.           node.style.left = dims.fixedPos + "px";
  642.           if (!widget.coverGutter) {
  643.             width -= dims.gutterTotalWidth;
  644.             node.style.paddingLeft = dims.gutterTotalWidth + "px";
  645.           }
  646.           node.style.width = width + "px";
  647.         }
  648.         if (widget.coverGutter) {
  649.           node.style.zIndex = 5;
  650.           node.style.position = "relative";
  651.           if (!widget.noHScroll) node.style.marginLeft = -dims.gutterTotalWidth + "px";
  652.         }
  653.         if (widget.above)
  654.           wrap.insertBefore(node, cm.options.lineNumbers && line.height != 0 ? gutterWrap : lineElement);
  655.         else
  656.           wrap.appendChild(node);
  657.       }
  658.  
  659.     if (ie_lt8) wrap.style.zIndex = 2;
  660.     return wrap;
  661.   }
  662.  
  663.   // SELECTION / CURSOR
  664.  
  665.   function selHead(view) {
  666.     return view.sel.inverted ? view.sel.from : view.sel.to;
  667.   }
  668.  
  669.   function updateSelection(cm) {
  670.     var headPos = posEq(cm.view.sel.from, cm.view.sel.to) ?
  671.       updateSelectionCursor(cm) :
  672.       updateSelectionRange(cm);
  673.     var display = cm.display;
  674.     // Move the hidden textarea near the cursor to prevent scrolling artifacts
  675.     var wrapOff = display.wrapper.getBoundingClientRect(), lineOff = display.lineDiv.getBoundingClientRect();
  676.     display.inputDiv.style.top = Math.max(0, Math.min(display.wrapper.clientHeight - 10,
  677.                                                       headPos.top + lineOff.top - wrapOff.top)) + "px";
  678.     display.inputDiv.style.left = Math.max(0, Math.min(display.wrapper.clientWidth - 10,
  679.                                                        headPos.left + lineOff.left - wrapOff.left)) + "px";
  680.   }
  681.  
  682.   // No selection, plain cursor
  683.   function updateSelectionCursor(cm) {
  684.     var display = cm.display, pos = cursorCoords(cm, cm.view.sel.from, "div");
  685.     display.cursor.style.left = pos.left + "px";
  686.     display.cursor.style.top = pos.top + "px";
  687.     display.cursor.style.height = Math.max(0, pos.bottom - pos.top) * cm.options.cursorHeight + "px";
  688.     display.cursor.style.display = "";
  689.     display.selectionDiv.style.display = "none";
  690.  
  691.     if (pos.other) {
  692.       display.otherCursor.style.display = "";
  693.       display.otherCursor.style.left = pos.other.left + "px";
  694.       display.otherCursor.style.top = pos.other.top + "px";
  695.       display.otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px";
  696.     } else { display.otherCursor.style.display = "none"; }
  697.     return pos;
  698.   }
  699.  
  700.   // Highlight selection
  701.   function updateSelectionRange(cm) {
  702.     var display = cm.display, doc = cm.view.doc, sel = cm.view.sel;
  703.     var fragment = document.createDocumentFragment();
  704.     var clientWidth = display.lineSpace.offsetWidth, pl = paddingLeft(cm.display);
  705.  
  706.     function add(left, top, width, bottom) {
  707.       if (top < 0) top = 0;
  708.       fragment.appendChild(elt("div", null, "CodeMirror-selected", "position: absolute; left: " + left +
  709.                                "px; top: " + top + "px; width: " + (width == null ? clientWidth - left : width) +
  710.                                "px; height: " + (bottom - top) + "px"));
  711.     }
  712.  
  713.     function drawForLine(line, fromArg, toArg, retTop) {
  714.       var lineObj = getLine(doc, line);
  715.       var lineLen = lineObj.text.length, rVal = retTop ? Infinity : -Infinity;
  716.       function coords(ch) {
  717.         return charCoords(cm, {line: line, ch: ch}, "div", lineObj);
  718.       }
  719.  
  720.       iterateBidiSections(getOrder(lineObj), fromArg || 0, toArg == null ? lineLen : toArg, function(from, to, dir) {
  721.         var leftPos = coords(dir == "rtl" ? to - 1 : from);
  722.         var rightPos = coords(dir == "rtl" ? from : to - 1);
  723.         var left = leftPos.left, right = rightPos.right;
  724.         if (rightPos.top - leftPos.top > 3) { // Different lines, draw top part
  725.           add(left, leftPos.top, null, leftPos.bottom);
  726.           left = pl;
  727.           if (leftPos.bottom < rightPos.top) add(left, leftPos.bottom, null, rightPos.top);
  728.         }
  729.         if (toArg == null && to == lineLen) right = clientWidth;
  730.         if (fromArg == null && from == 0) left = pl;
  731.         rVal = retTop ? Math.min(rightPos.top, rVal) : Math.max(rightPos.bottom, rVal);
  732.         if (left < pl + 1) left = pl;
  733.         add(left, rightPos.top, right - left, rightPos.bottom);
  734.       });
  735.       return rVal;
  736.     }
  737.  
  738.     if (sel.from.line == sel.to.line) {
  739.       drawForLine(sel.from.line, sel.from.ch, sel.to.ch);
  740.     } else {
  741.       var fromObj = getLine(doc, sel.from.line);
  742.       var cur = fromObj, merged, path = [sel.from.line, sel.from.ch], singleLine;
  743.       while (merged = collapsedSpanAtEnd(cur)) {
  744.         var found = merged.find();
  745.         path.push(found.from.ch, found.to.line, found.to.ch);
  746.         if (found.to.line == sel.to.line) {
  747.           path.push(sel.to.ch);
  748.           singleLine = true;
  749.           break;
  750.         }
  751.         cur = getLine(doc, found.to.line);
  752.       }
  753.  
  754.       // This is a single, merged line
  755.       if (singleLine) {
  756.         for (var i = 0; i < path.length; i += 3)
  757.           drawForLine(path[i], path[i+1], path[i+2]);
  758.       } else {
  759.         var middleTop, middleBot, toObj = getLine(doc, sel.to.line);
  760.         if (sel.from.ch)
  761.           // Draw the first line of selection.
  762.           middleTop = drawForLine(sel.from.line, sel.from.ch, null, false);
  763.         else
  764.           // Simply include it in the middle block.
  765.           middleTop = heightAtLine(cm, fromObj) - display.viewOffset;
  766.  
  767.         if (!sel.to.ch)
  768.           middleBot = heightAtLine(cm, toObj) - display.viewOffset;
  769.         else
  770.           middleBot = drawForLine(sel.to.line, collapsedSpanAtStart(toObj) ? null : 0, sel.to.ch, true);
  771.  
  772.         if (middleTop < middleBot) add(pl, middleTop, null, middleBot);
  773.       }
  774.     }
  775.  
  776.     removeChildrenAndAdd(display.selectionDiv, fragment);
  777.     display.cursor.style.display = display.otherCursor.style.display = "none";
  778.     display.selectionDiv.style.display = "";
  779.     return cursorCoords(cm, selHead(cm.view), "div");
  780.   }
  781.  
  782.   // Cursor-blinking
  783.   function restartBlink(cm) {
  784.     var display = cm.display;
  785.     clearInterval(display.blinker);
  786.     var on = true;
  787.     display.cursor.style.visibility = display.otherCursor.style.visibility = "";
  788.     display.blinker = setInterval(function() {
  789.       display.cursor.style.visibility = display.otherCursor.style.visibility = (on = !on) ? "" : "hidden";
  790.     }, cm.options.cursorBlinkRate);
  791.   }
  792.  
  793.   // HIGHLIGHT WORKER
  794.  
  795.   function startWorker(cm, time) {
  796.     if (cm.view.frontier < cm.display.showingTo)
  797.       cm.view.highlight.set(time, bind(highlightWorker, cm));
  798.   }
  799.  
  800.   function highlightWorker(cm) {
  801.     var view = cm.view, doc = view.doc;
  802.     if (view.frontier >= cm.display.showingTo) return;
  803.     var end = +new Date + cm.options.workTime;
  804.     var state = copyState(view.mode, getStateBefore(cm, view.frontier));
  805.     var changed = [], prevChange;
  806.     doc.iter(view.frontier, Math.min(doc.size, cm.display.showingTo + 500), function(line) {
  807.       if (view.frontier >= cm.display.showingFrom) { // Visible
  808.         if (highlightLine(cm, line, state) && view.frontier >= cm.display.showingFrom) {
  809.           if (prevChange && prevChange.end == view.frontier) prevChange.end++;
  810.           else changed.push(prevChange = {start: view.frontier, end: view.frontier + 1});
  811.         }
  812.         line.stateAfter = copyState(view.mode, state);
  813.       } else {
  814.         processLine(cm, line, state);
  815.         line.stateAfter = view.frontier % 5 == 0 ? copyState(view.mode, state) : null;
  816.       }
  817.       ++view.frontier;
  818.       if (+new Date > end) {
  819.         startWorker(cm, cm.options.workDelay);
  820.         return true;
  821.       }
  822.     });
  823.     if (changed.length)
  824.       operation(cm, function() {
  825.         for (var i = 0; i < changed.length; ++i)
  826.           regChange(this, changed[i].start, changed[i].end);
  827.       })();
  828.   }
  829.  
  830.   // Finds the line to start with when starting a parse. Tries to
  831.   // find a line with a stateAfter, so that it can start with a
  832.   // valid state. If that fails, it returns the line with the
  833.   // smallest indentation, which tends to need the least context to
  834.   // parse correctly.
  835.   function findStartLine(cm, n) {
  836.     var minindent, minline, doc = cm.view.doc;
  837.     for (var search = n, lim = n - 100; search > lim; --search) {
  838.       if (search == 0) return 0;
  839.       var line = getLine(doc, search-1);
  840.       if (line.stateAfter) return search;
  841.       var indented = countColumn(line.text, null, cm.options.tabSize);
  842.       if (minline == null || minindent > indented) {
  843.         minline = search - 1;
  844.         minindent = indented;
  845.       }
  846.     }
  847.     return minline;
  848.   }
  849.  
  850.   function getStateBefore(cm, n) {
  851.     var view = cm.view;
  852.     var pos = findStartLine(cm, n), state = pos && getLine(view.doc, pos-1).stateAfter;
  853.     if (!state) state = startState(view.mode);
  854.     else state = copyState(view.mode, state);
  855.     view.doc.iter(pos, n, function(line) {
  856.       processLine(cm, line, state);
  857.       var save = pos == n - 1 || pos % 5 == 0 || pos >= view.showingFrom && pos < view.showingTo;
  858.       line.stateAfter = save ? copyState(view.mode, state) : null;
  859.       ++pos;
  860.     });
  861.     return state;
  862.   }
  863.  
  864.   // POSITION MEASUREMENT
  865.   
  866.   function paddingTop(display) {return display.lineSpace.offsetTop;}
  867.   function paddingLeft(display) {
  868.     var e = removeChildrenAndAdd(display.measure, elt("pre")).appendChild(elt("span", "x"));
  869.     return e.offsetLeft;
  870.   }
  871.  
  872.   function measureChar(cm, line, ch, data) {
  873.     var data = data || measureLine(cm, line);
  874.     var dir = -1, len = line.text.length;
  875.     for (var pos = ch;; pos += dir) {
  876.       var r = data[pos];
  877.       if (r) break;
  878.       if (dir < 0 && pos == 0) dir = 1;
  879.     }
  880.     return {left: pos < ch ? r.right : r.left,
  881.             right: pos > ch ? r.left : r.right,
  882.             top: r.top, bottom: r.bottom};
  883.   }
  884.  
  885.   function measureLine(cm, line) {
  886.     // First look in the cache
  887.     var display = cm.display, cache = cm.display.measureLineCache;
  888.     for (var i = 0; i < cache.length; ++i) {
  889.       var memo = cache[i];
  890.       if (memo.text == line.text && memo.markedSpans == line.markedSpans &&
  891.           display.scroller.clientWidth == memo.width)
  892.         return memo.measure;
  893.     }
  894.     
  895.     var measure = measureLineInner(cm, line);
  896.     // Store result in the cache
  897.     var memo = {text: line.text, width: display.scroller.clientWidth,
  898.                 markedSpans: line.markedSpans, measure: measure};
  899.     if (cache.length == 16) cache[++cache.pos % 16] = memo;
  900.     else cache.push(memo);
  901.     return measure;
  902.   }
  903.  
  904.   function measureLineInner(cm, line) {
  905.     var display = cm.display, measure = emptyArray(line.text.length);
  906.     var pre = lineContent(cm, line, measure);
  907.     removeChildrenAndAdd(display.measure, pre);
  908.  
  909.     var outer = display.lineDiv.getBoundingClientRect();
  910.     var vranges = [], data = emptyArray(line.text.length), maxBot = pre.offsetHeight;
  911.     for (var i = 0, elt; i < measure.length; ++i) if (elt = measure[i]) {
  912.       var size = measure[i].getBoundingClientRect();
  913.       var top = Math.max(0, size.top - outer.top), bot = Math.min(size.bottom - outer.top, maxBot);
  914.       for (var j = 0; j < vranges.length; j += 2) {
  915.         var rtop = vranges[j], rbot = vranges[j+1];
  916.         if (rtop > bot || rbot < top) continue;
  917.         if (rtop <= top && rbot >= bot ||
  918.             top <= rtop && bot >= rbot ||
  919.             Math.min(bot, rbot) - Math.max(top, rtop) >= (bot - top) >> 1) {
  920.           vranges[j] = Math.min(top, rtop);
  921.           vranges[j+1] = Math.max(bot, rbot);
  922.           break;
  923.         }
  924.       }
  925.       if (j == vranges.length) vranges.push(top, bot);
  926.       data[i] = {left: size.left - outer.left, right: size.right - outer.left, top: j};
  927.     }
  928.     for (var i = 0, elt; i < data.length; ++i) if (elt = data[i]) {
  929.       var vr = elt.top;
  930.       elt.top = vranges[vr]; elt.bottom = vranges[vr+1];
  931.     }
  932.     return data;
  933.   }
  934.  
  935.   // Context is one of "line", "div" (display.lineDiv), "local"/null (editor), or "page"
  936.   function intoCoordSystem(cm, lineObj, pos, rect, context) {
  937.     if (lineObj.widgets) for (var i = 0; i < lineObj.widgets.length; ++i) if (lineObj.widgets[i].above) {
  938.       var size = lineObj.widgets[i].node.offsetHeight;
  939.       rect.top += size; rect.bottom += size;
  940.     }
  941.     if (context == "line") return rect;
  942.     if (!context) context = "local";
  943.     var yOff = heightAtLine(cm, lineObj);
  944.     if (context != "local") yOff -= cm.display.viewOffset;
  945.     if (context == "page") {
  946.       var lOff = cm.display.lineSpace.getBoundingClientRect();
  947.       yOff += lOff.top + (window.pageYOffset || (document.documentElement || document.body).scrollTop);
  948.       var xOff = lOff.left + (window.pageXOffset || (document.documentElement || document.body).scrollLeft);
  949.       rect.left += xOff; rect.right += xOff;
  950.     }
  951.     rect.top += yOff; rect.bottom += yOff;
  952.     return rect;
  953.   }
  954.  
  955.   function charCoords(cm, pos, context, lineObj) {
  956.     if (!lineObj) lineObj = getLine(cm.view.doc, pos.line);
  957.     return intoCoordSystem(cm, lineObj, pos, measureChar(cm, lineObj, pos.ch), context);
  958.   }
  959.  
  960.   function cursorCoords(cm, pos, context, lineObj, measurement) {
  961.     lineObj = lineObj || getLine(cm.view.doc, pos.line);
  962.     if (!measurement) measurement = measureLine(cm, lineObj);
  963.     function get(ch, right) {
  964.       var m = measureChar(cm, lineObj, ch, measurement);
  965.       if (right) m.left = m.right; else m.right = m.left;
  966.       return intoCoordSystem(cm, lineObj, pos, m, context);
  967.     }
  968.     var order = getOrder(lineObj), ch = pos.ch;
  969.     if (!order) return get(ch);
  970.     var main, other, linedir = order[0].level;
  971.     for (var i = 0; i < order.length; ++i) {
  972.       var part = order[i], rtl = part.level % 2, nb, here;
  973.       if (part.from < ch && part.to > ch) return get(ch, rtl);
  974.       var left = rtl ? part.to : part.from, right = rtl ? part.from : part.to;
  975.       if (left == ch) {
  976.         // Opera and IE return bogus offsets and widths for edges
  977.         // where the direction flips, but only for the side with the
  978.         // lower level. So we try to use the side with the higher
  979.         // level.
  980.         if (i && part.level < (nb = order[i-1]).level) here = get(nb.level % 2 ? nb.from : nb.to - 1, true);
  981.         else here = get(rtl && part.from != part.to ? ch - 1 : ch);
  982.         if (rtl == linedir) main = here; else other = here;
  983.       } else if (right == ch) {
  984.         var nb = i < order.length - 1 && order[i+1];
  985.         if (!rtl && nb && nb.from == nb.to) continue;
  986.         if (nb && part.level < nb.level) here = get(nb.level % 2 ? nb.to - 1 : nb.from);
  987.         else here = get(rtl ? ch : ch - 1, true);
  988.         if (rtl == linedir) main = here; else other = here;
  989.       }
  990.     }
  991.     if (linedir && !ch) other = get(order[0].to - 1);
  992.     if (!main) return other;
  993.     if (other) main.other = other;
  994.     return main;
  995.   }
  996.  
  997.   // Coords must be lineSpace-local
  998.   function coordsChar(cm, x, y) {
  999.     var doc = cm.view.doc;
  1000.     y += cm.display.viewOffset;
  1001.     if (y < 0) return {line: 0, ch: 0, outside: true};
  1002.     var lineNo = lineAtHeight(doc, y);
  1003.     if (lineNo >= doc.size) return {line: doc.size - 1, ch: getLine(doc, doc.size - 1).text.length};
  1004.     if (x < 0) x = 0;
  1005.  
  1006.     for (;;) {
  1007.       var lineObj = getLine(doc, lineNo);
  1008.       var found = coordsCharInner(cm, lineObj, lineNo, x, y);
  1009.       var merged = collapsedSpanAtEnd(lineObj);
  1010.       if (merged && found.ch == lineRight(lineObj))
  1011.         lineNo = merged.find().to.line;
  1012.       else
  1013.         return found;
  1014.     }
  1015.   }
  1016.  
  1017.   function coordsCharInner(cm, lineObj, lineNo, x, y) {
  1018.     var doc = cm.view.doc;
  1019.     var innerOff = y - heightAtLine(cm, lineObj);
  1020.     var wrongLine = false, cWidth = cm.display.wrapper.clientWidth;
  1021.     var measurement = measureLine(cm, lineObj);
  1022.  
  1023.     function getX(ch) {
  1024.       var sp = cursorCoords(cm, {line: lineNo, ch: ch}, "line",
  1025.                             lineObj, measurement);
  1026.       wrongLine = true;
  1027.       if (innerOff > sp.bottom) return Math.max(0, sp.left - cWidth);
  1028.       else if (innerOff < sp.top) return sp.left + cWidth;
  1029.       else wrongLine = false;
  1030.       return sp.left;
  1031.     }
  1032.  
  1033.     var bidi = getOrder(lineObj), dist = lineObj.text.length;
  1034.     var from = lineLeft(lineObj), to = lineRight(lineObj), fromX = paddingLeft(cm.display), toX;
  1035.     if (!bidi) {
  1036.       // Guess a suitable upper bound for our search.
  1037.       var estimated = Math.min(to, Math.ceil((x + Math.floor(innerOff / textHeight(cm.display)) *
  1038.                                               cWidth * .9) / charWidth(cm.display)));
  1039.       for (;;) {
  1040.         var estX = getX(estimated);
  1041.         if (estX <= x && estimated < to) estimated = Math.min(to, Math.ceil(estimated * 1.2));
  1042.         else {toX = estX; to = estimated; break;}
  1043.       }
  1044.       // Try to guess a suitable lower bound as well.
  1045.       estimated = Math.floor(to * 0.8); estX = getX(estimated);
  1046.       if (estX < x) {from = estimated; fromX = estX;}
  1047.       dist = to - from;
  1048.     } else toX = getX(to);
  1049.     if (x > toX) return {line: lineNo, ch: to, outside: wrongLine};
  1050.     // Do a binary search between these bounds.
  1051.     for (;;) {
  1052.       if (bidi ? to == from || to == moveVisually(lineObj, from, 1) : to - from <= 1) {
  1053.         var after = x - fromX < toX - x, ch = after ? from : to;
  1054.         while (isExtendingChar.test(lineObj.text.charAt(ch))) ++ch;
  1055.         return {line: lineNo, ch: ch, after: after, outside: wrongLine};
  1056.       }
  1057.       var step = Math.ceil(dist / 2), middle = from + step;
  1058.       if (bidi) {
  1059.         middle = from;
  1060.         for (var i = 0; i < step; ++i) middle = moveVisually(lineObj, middle, 1);
  1061.       }
  1062.       var middleX = getX(middle);
  1063.       if (middleX > x) {to = middle; toX = middleX; if (wrongLine) toX += 1000; dist -= step;}
  1064.       else {from = middle; fromX = middleX; dist = step;}
  1065.     }
  1066.   }
  1067.  
  1068.   var measureText;
  1069.   function textHeight(display) {
  1070.     if (display.cachedTextHeight != null) return display.cachedTextHeight;
  1071.     if (measureText == null) {
  1072.       measureText = elt("pre");
  1073.       // Measure a bunch of lines, for browsers that compute
  1074.       // fractional heights.
  1075.       for (var i = 0; i < 49; ++i) {
  1076.         measureText.appendChild(document.createTextNode("x"));
  1077.         measureText.appendChild(elt("br"));
  1078.       }
  1079.       measureText.appendChild(document.createTextNode("x"));
  1080.     }
  1081.     removeChildrenAndAdd(display.measure, measureText);
  1082.     var height = measureText.offsetHeight / 50;
  1083.     if (height > 3) display.cachedTextHeight = height;
  1084.     removeChildren(display.measure);
  1085.     return height || 1;
  1086.   }
  1087.  
  1088.   function charWidth(display) {
  1089.     if (display.cachedCharWidth != null) return display.cachedCharWidth;
  1090.     var anchor = elt("span", "x");
  1091.     var pre = elt("pre", [anchor]);
  1092.     removeChildrenAndAdd(display.measure, pre);
  1093.     var width = anchor.offsetWidth;
  1094.     if (width > 2) display.cachedCharWidth = width;
  1095.     return width || 10;
  1096.   }
  1097.  
  1098.   // OPERATIONS
  1099.  
  1100.   // Operations are used to wrap changes in such a way that each
  1101.   // change won't have to update the cursor and display (which would
  1102.   // be awkward, slow, and error-prone), but instead updates are
  1103.   // batched and then all combined and executed at once.
  1104.  
  1105.   function startOperation(cm) {
  1106.     if (cm.curOp) ++cm.curOp.depth;
  1107.     else cm.curOp = {
  1108.       // Nested operations delay update until the outermost one
  1109.       // finishes.
  1110.       depth: 1,
  1111.       // An array of ranges of lines that have to be updated. See
  1112.       // updateDisplay.
  1113.       changes: [],
  1114.       delayedCallbacks: [],
  1115.       updateInput: null,
  1116.       userSelChange: null,
  1117.       textChanged: null,
  1118.       selectionChanged: false,
  1119.       updateMaxLine: false
  1120.     };
  1121.   }
  1122.  
  1123.   function endOperation(cm) {
  1124.     var op = cm.curOp;
  1125.     if (--op.depth) return;
  1126.     cm.curOp = null;
  1127.     var view = cm.view, display = cm.display;
  1128.     if (op.updateMaxLine) computeMaxLength(view);
  1129.     if (view.maxLineChanged && !cm.options.lineWrapping) {
  1130.       var width = measureChar(cm, view.maxLine, view.maxLine.text.length).right;
  1131.       display.sizer.style.minWidth = (width + 3 + scrollerCutOff) + "px";
  1132.       view.maxLineChanged = false;
  1133.     }
  1134.     var newScrollPos, updated;
  1135.     if (op.selectionChanged) {
  1136.       var coords = cursorCoords(cm, selHead(view));
  1137.       newScrollPos = calculateScrollPos(display, coords.left, coords.top, coords.left, coords.bottom);
  1138.     }
  1139.     if (op.changes.length || newScrollPos && newScrollPos.scrollTop != null)
  1140.       updated = updateDisplay(cm, op.changes, newScrollPos && newScrollPos.scrollTop);
  1141.     if (!updated && op.selectionChanged) updateSelection(cm);
  1142.     if (newScrollPos) scrollCursorIntoView(cm);
  1143.     if (op.selectionChanged) restartBlink(cm);
  1144.  
  1145.     if (view.focused && op.updateInput)
  1146.       resetInput(cm, op.userSelChange);
  1147.  
  1148.     if (op.textChanged)
  1149.       signal(cm, "change", cm, op.textChanged);
  1150.     if (op.selectionChanged) signal(cm, "cursorActivity", cm);
  1151.     for (var i = 0; i < op.delayedCallbacks.length; ++i) op.delayedCallbacks[i](cm);
  1152.   }
  1153.  
  1154.   // Wraps a function in an operation. Returns the wrapped function.
  1155.   function operation(cm1, f) {
  1156.     return function() {
  1157.       var cm = cm1 || this;
  1158.       startOperation(cm);
  1159.       try {var result = f.apply(cm, arguments);}
  1160.       finally {endOperation(cm);}
  1161.       return result;
  1162.     };
  1163.   }
  1164.  
  1165.   function regChange(cm, from, to, lendiff) {
  1166.     cm.curOp.changes.push({from: from, to: to, diff: lendiff});
  1167.   }
  1168.  
  1169.   // INPUT HANDLING
  1170.  
  1171.   function slowPoll(cm) {
  1172.     if (cm.view.pollingFast) return;
  1173.     cm.display.poll.set(cm.options.pollInterval, function() {
  1174.       readInput(cm);
  1175.       if (cm.view.focused) slowPoll(cm);
  1176.     });
  1177.   }
  1178.  
  1179.   function fastPoll(cm) {
  1180.     var missed = false;
  1181.     cm.display.pollingFast = true;
  1182.     function p() {
  1183.       var changed = readInput(cm);
  1184.       if (!changed && !missed) {missed = true; cm.display.poll.set(60, p);}
  1185.       else {cm.display.pollingFast = false; slowPoll(cm);}
  1186.     }
  1187.     cm.display.poll.set(20, p);
  1188.   }
  1189.  
  1190.   // prevInput is a hack to work with IME. If we reset the textarea
  1191.   // on every change, that breaks IME. So we look for changes
  1192.   // compared to the previous content instead. (Modern browsers have
  1193.   // events that indicate IME taking place, but these are not widely
  1194.   // supported or compatible enough yet to rely on.)
  1195.   function readInput(cm) {
  1196.     var input = cm.display.input, prevInput = cm.display.prevInput, view = cm.view, sel = view.sel;
  1197.     if (!view.focused || hasSelection(input) || isReadOnly(cm)) return false;
  1198.     var text = input.value;
  1199.     if (text == prevInput && posEq(sel.from, sel.to)) return false;
  1200.     startOperation(cm);
  1201.     view.sel.shift = null;
  1202.     var same = 0, l = Math.min(prevInput.length, text.length);
  1203.     while (same < l && prevInput[same] == text[same]) ++same;
  1204.     if (same < prevInput.length)
  1205.       sel.from = {line: sel.from.line, ch: sel.from.ch - (prevInput.length - same)};
  1206.     else if (view.overwrite && posEq(sel.from, sel.to) && !cm.display.pasteIncoming)
  1207.       sel.to = {line: sel.to.line, ch: Math.min(getLine(cm.view.doc, sel.to.line).text.length, sel.to.ch + (text.length - same))};
  1208.     var updateInput = cm.curOp.updateInput;
  1209.     cm.replaceSelection(text.slice(same), "end");
  1210.     cm.curOp.updateInput = updateInput;
  1211.     if (text.length > 1000) { input.value = cm.display.prevInput = ""; }
  1212.     else cm.display.prevInput = text;
  1213.     endOperation(cm);
  1214.     cm.display.pasteIncoming = false;
  1215.     return true;
  1216.   }
  1217.  
  1218.   function resetInput(cm, user) {
  1219.     var view = cm.view, minimal, selected;
  1220.     if (!posEq(view.sel.from, view.sel.to)) {
  1221.       cm.display.prevInput = "";
  1222.       minimal = hasCopyEvent &&
  1223.         (view.sel.to.line - view.sel.from.line > 100 || (selected = cm.getSelection()).length > 1000);
  1224.       if (minimal) cm.display.input.value = "-";
  1225.       else cm.display.input.value = selected || cm.getSelection();
  1226.       if (view.focused) selectInput(cm.display.input);
  1227.     } else if (user) cm.display.prevInput = cm.display.input.value = "";
  1228.     cm.display.inaccurateSelection = minimal;
  1229.   }
  1230.  
  1231.   function focusInput(cm) {
  1232.     if (cm.options.readOnly != "nocursor") cm.display.input.focus();
  1233.   }
  1234.  
  1235.   function isReadOnly(cm) {
  1236.     return cm.options.readOnly || cm.view.cantEdit;
  1237.   }
  1238.  
  1239.   // EVENT HANDLERS
  1240.  
  1241.   function registerEventHandlers(cm) {
  1242.     var d = cm.display;
  1243.     on(d.scroller, "mousedown", operation(cm, onMouseDown));
  1244.     on(d.gutters, "mousedown", operation(cm, clickInGutter));
  1245.     on(d.scroller, "dblclick", operation(cm, e_preventDefault));
  1246.     on(d.lineSpace, "selectstart", function(e) {
  1247.       if (!mouseEventInWidget(d, e)) e_preventDefault(e);
  1248.     });
  1249.     // Gecko browsers fire contextmenu *after* opening the menu, at
  1250.     // which point we can't mess with it anymore. Context menu is
  1251.     // handled in onMouseDown for Gecko.
  1252.     if (!gecko) on(d.scroller, "contextmenu", function(e) {onContextMenu(cm, e);});
  1253.  
  1254.     on(d.scroller, "scroll", function() {
  1255.       setScrollTop(cm, d.scroller.scrollTop);
  1256.       setScrollLeft(cm, d.scroller.scrollLeft);
  1257.       signal(cm, "scroll", cm);
  1258.     });
  1259.     on(d.scrollbarV, "scroll", function() {
  1260.       setScrollTop(cm, d.scrollbarV.scrollTop);
  1261.     });
  1262.     on(d.scrollbarH, "scroll", function() {
  1263.       setScrollLeft(cm, d.scrollbarH.scrollLeft);
  1264.     });
  1265.  
  1266.     on(d.scroller, "mousewheel", function(e){onScrollWheel(cm, e);});
  1267.     on(d.scroller, "DOMMouseScroll", function(e){onScrollWheel(cm, e);});
  1268.  
  1269.     function reFocus() { if (cm.view.focused) setTimeout(bind(focusInput, cm), 0); }
  1270.     on(d.scrollbarH, "mousedown", reFocus);
  1271.     on(d.scrollbarV, "mousedown", reFocus);
  1272.     // Prevent wrapper from ever scrolling
  1273.     on(d.wrapper, "scroll", function() { d.wrapper.scrollTop = d.wrapper.scrollLeft = 0; });
  1274.     on(window, "resize", function resizeHandler() {
  1275.       // Might be a text scaling operation, clear size caches.
  1276.       d.cachedCharWidth = d.cachedTextHeight = null;
  1277.       d.measureLineCache.length = d.measureLineCache.pos = 0;
  1278.       if (d.wrapper.parentNode) updateDisplay(cm, true);
  1279.       else off(window, "resize", resizeHandler);
  1280.     });
  1281.  
  1282.     on(d.input, "keyup", operation(cm, function(e) {
  1283.       if (cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;
  1284.       if (e_prop(e, "keyCode") == 16) cm.view.sel.shift = null;
  1285.     }));
  1286.     on(d.input, "input", bind(fastPoll, cm));
  1287.     on(d.input, "keydown", operation(cm, onKeyDown));
  1288.     on(d.input, "keypress", operation(cm, onKeyPress));
  1289.     on(d.input, "focus", bind(onFocus, cm));
  1290.     on(d.input, "blur", bind(onBlur, cm));
  1291.  
  1292.     function drag_(e) {
  1293.       if (cm.options.onDragEvent && cm.options.onDragEvent(cm, addStop(e))) return;
  1294.       e_stop(e);
  1295.     }
  1296.     if (cm.options.dragDrop) {
  1297.       on(d.scroller, "dragstart", function(e){onDragStart(cm, e);});
  1298.       on(d.scroller, "dragenter", drag_);
  1299.       on(d.scroller, "dragover", drag_);
  1300.       on(d.scroller, "drop", operation(cm, onDrop));
  1301.     }
  1302.     on(d.scroller, "paste", function(){focusInput(cm); fastPoll(cm);});
  1303.     on(d.input, "paste", function() {
  1304.       d.pasteIncoming = true;
  1305.       fastPoll(cm);
  1306.     });
  1307.  
  1308.     function prepareCopy() {
  1309.       if (d.inaccurateSelection) {
  1310.         d.prevInput = "";
  1311.         d.inaccurateSelection = false;
  1312.         d.input.value = cm.getSelection();
  1313.         selectInput(d.input);
  1314.       }
  1315.     }
  1316.     on(d.input, "cut", prepareCopy);
  1317.     on(d.input, "copy", prepareCopy);
  1318.  
  1319.     // Needed to handle Tab key in KHTML
  1320.     if (khtml) on(d.sizer, "mouseup", function() {
  1321.         if (document.activeElement == d.input) d.input.blur();
  1322.         focusInput(cm);
  1323.     });
  1324.   }
  1325.  
  1326.   function mouseEventInWidget(display, e) {
  1327.     for (var n = e_target(e); n != display.wrapper; n = n.parentNode)
  1328.       if (/\bCodeMirror-(?:line)?widget\b/.test(n.className) ||
  1329.           n.parentNode == display.sizer && n != display.mover) return true;
  1330.   }
  1331.  
  1332.   function posFromMouse(cm, e, liberal) {
  1333.     var display = cm.display;
  1334.     if (!liberal) {
  1335.       var target = e_target(e);
  1336.       if (target == display.scrollbarH || target == display.scrollbarH.firstChild ||
  1337.           target == display.scrollbarV || target == display.scrollbarV.firstChild ||
  1338.           target == display.scrollbarFiller) return null;
  1339.     }
  1340.     var x, y, space = display.lineSpace.getBoundingClientRect();
  1341.     // Fails unpredictably on IE[67] when mouse is dragged around quickly.
  1342.     try { x = e.clientX; y = e.clientY; } catch (e) { return null; }
  1343.     return coordsChar(cm, x - space.left, y - space.top);
  1344.   }
  1345.  
  1346.   var lastClick, lastDoubleClick;
  1347.   function onMouseDown(e) {
  1348.     var cm = this, display = cm.display, view = cm.view, sel = view.sel, doc = view.doc;
  1349.     setShift(cm.view, e_prop(e, "shiftKey"));
  1350.  
  1351.     if (mouseEventInWidget(display, e)) {
  1352.       if (!webkit) {
  1353.         display.scroller.draggable = false;
  1354.         setTimeout(function(){display.scroller.draggable = true;}, 100);
  1355.       }
  1356.       return;
  1357.     }
  1358.     if (clickInGutter.call(cm, e)) return;
  1359.     var start = posFromMouse(cm, e);
  1360.  
  1361.     switch (e_button(e)) {
  1362.     case 3:
  1363.       if (gecko) onContextMenu.call(cm, cm, e);
  1364.       return;
  1365.     case 2:
  1366.       if (start) setSelectionUser(cm, start, start);
  1367.       setTimeout(bind(focusInput, cm), 20);
  1368.       e_preventDefault(e);
  1369.       return;
  1370.     }
  1371.     // For button 1, if it was clicked inside the editor
  1372.     // (posFromMouse returning non-null), we have to adjust the
  1373.     // selection.
  1374.     if (!start) {if (e_target(e) == display.scroller) e_preventDefault(e); return;}
  1375.  
  1376.     if (!view.focused) onFocus(cm);
  1377.  
  1378.     var now = +new Date, type = "single";
  1379.     if (lastDoubleClick && lastDoubleClick.time > now - 400 && posEq(lastDoubleClick.pos, start)) {
  1380.       type = "triple";
  1381.       e_preventDefault(e);
  1382.       setTimeout(bind(focusInput, cm), 20);
  1383.       selectLine(cm, start.line);
  1384.     } else if (lastClick && lastClick.time > now - 400 && posEq(lastClick.pos, start)) {
  1385.       type = "double";
  1386.       lastDoubleClick = {time: now, pos: start};
  1387.       e_preventDefault(e);
  1388.       var word = findWordAt(getLine(doc, start.line).text, start);
  1389.       setSelectionUser(cm, word.from, word.to);
  1390.     } else { lastClick = {time: now, pos: start}; }
  1391.  
  1392.     var last = start;
  1393.     if (cm.options.dragDrop && dragAndDrop && !isReadOnly(cm) && !posEq(sel.from, sel.to) &&
  1394.         !posLess(start, sel.from) && !posLess(sel.to, start) && type == "single") {
  1395.       var dragEnd = operation(cm, function(e2) {
  1396.         if (webkit) display.scroller.draggable = false;
  1397.         view.draggingText = false;
  1398.         off(document, "mouseup", dragEnd);
  1399.         off(display.scroller, "drop", dragEnd);
  1400.         if (Math.abs(e.clientX - e2.clientX) + Math.abs(e.clientY - e2.clientY) < 10) {
  1401.           e_preventDefault(e2);
  1402.           setSelectionUser(cm, start, start);
  1403.           focusInput(cm);
  1404.         }
  1405.       });
  1406.       // Let the drag handler handle this.
  1407.       if (webkit) display.scroller.draggable = true;
  1408.       view.draggingText = dragEnd;
  1409.       // IE's approach to draggable
  1410.       if (display.scroller.dragDrop) display.scroller.dragDrop();
  1411.       on(document, "mouseup", dragEnd);
  1412.       on(display.scroller, "drop", dragEnd);
  1413.       return;
  1414.     }
  1415.     e_preventDefault(e);
  1416.     if (type == "single") setSelectionUser(cm, start, start);
  1417.  
  1418.     var startstart = sel.from, startend = sel.to;
  1419.  
  1420.     function doSelect(cur) {
  1421.       if (type == "single") {
  1422.         setSelectionUser(cm, start, cur);
  1423.       } else if (type == "double") {
  1424.         var word = findWordAt(getLine(doc, cur.line).text, cur);
  1425.         if (posLess(cur, startstart)) setSelectionUser(cm, word.from, startend);
  1426.         else setSelectionUser(cm, startstart, word.to);
  1427.       } else if (type == "triple") {
  1428.         if (posLess(cur, startstart)) setSelectionUser(cm, startend, clipPos(doc, {line: cur.line, ch: 0}));
  1429.         else setSelectionUser(cm, startstart, clipPos(doc, {line: cur.line + 1, ch: 0}));
  1430.       }
  1431.     }
  1432.  
  1433.     var editorSize = display.wrapper.getBoundingClientRect();
  1434.     // Used to ensure timeout re-tries don't fire when another extend
  1435.     // happened in the meantime (clearTimeout isn't reliable -- at
  1436.     // least on Chrome, the timeouts still happen even when cleared,
  1437.     // if the clear happens after their scheduled firing time).
  1438.     var counter = 0;
  1439.  
  1440.     function extend(e) {
  1441.       var curCount = ++counter;
  1442.       var cur = posFromMouse(cm, e, true);
  1443.       if (!cur) return;
  1444.       if (!posEq(cur, last)) {
  1445.         if (!view.focused) onFocus(cm);
  1446.         last = cur;
  1447.         doSelect(cur);
  1448.         var visible = visibleLines(display, doc);
  1449.         if (cur.line >= visible.to || cur.line < visible.from)
  1450.           setTimeout(operation(cm, function(){if (counter == curCount) extend(e);}), 150);
  1451.       } else {
  1452.         var outside = e.clientY < editorSize.top ? -20 : e.clientY > editorSize.bottom ? 20 : 0;
  1453.         if (outside) setTimeout(operation(cm, function() {
  1454.           if (counter != curCount) return;
  1455.           display.scroller.scrollTop += outside;
  1456.           extend(e);
  1457.         }), 50);
  1458.       }
  1459.     }
  1460.  
  1461.     function done(e) {
  1462.       counter = Infinity;
  1463.       var cur = posFromMouse(cm, e);
  1464.       if (cur) doSelect(cur);
  1465.       e_preventDefault(e);
  1466.       focusInput(cm);
  1467.       off(document, "mousemove", move);
  1468.       off(document, "mouseup", up);
  1469.     }
  1470.  
  1471.     var move = operation(cm, function(e) {
  1472.       e_preventDefault(e);
  1473.       if (!ie && !e_button(e)) done(e);
  1474.       else extend(e);
  1475.     });
  1476.     var up = operation(cm, done);
  1477.     on(document, "mousemove", move);
  1478.     on(document, "mouseup", up);
  1479.   }
  1480.  
  1481.   function onDrop(e) {
  1482.     var cm = this;
  1483.     if (cm.options.onDragEvent && cm.options.onDragEvent(cm, addStop(e))) return;
  1484.     e_preventDefault(e);
  1485.     var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files;
  1486.     if (!pos || isReadOnly(cm)) return;
  1487.     if (files && files.length && window.FileReader && window.File) {
  1488.       var n = files.length, text = Array(n), read = 0;
  1489.       var loadFile = function(file, i) {
  1490.         var reader = new FileReader;
  1491.         reader.onload = function() {
  1492.           text[i] = reader.result;
  1493.           if (++read == n) {
  1494.             pos = clipPos(cm.view.doc, pos);
  1495.             operation(cm, function() {
  1496.               var end = replaceRange(cm, text.join(""), pos, pos);
  1497.               setSelectionUser(cm, pos, end);
  1498.             })();
  1499.           }
  1500.         };
  1501.         reader.readAsText(file);
  1502.       };
  1503.       for (var i = 0; i < n; ++i) loadFile(files[i], i);
  1504.     } else {
  1505.       // Don't do a replace if the drop happened inside of the selected text.
  1506.       if (cm.view.draggingText && !(posLess(pos, cm.view.sel.from) || posLess(cm.view.sel.to, pos))) {
  1507.         cm.view.draggingText(e);
  1508.         if (ie) setTimeout(bind(focusInput, cm), 50);
  1509.         return;
  1510.       }
  1511.       try {
  1512.         var text = e.dataTransfer.getData("Text");
  1513.         if (text) {
  1514.           compoundChange(cm, function() {
  1515.             var curFrom = cm.view.sel.from, curTo = cm.view.sel.to;
  1516.             setSelectionUser(cm, pos, pos);
  1517.             if (cm.view.draggingText) replaceRange(cm, "", curFrom, curTo);
  1518.             cm.replaceSelection(text);
  1519.             focusInput(cm);
  1520.             onFocus(cm);
  1521.           });
  1522.         }
  1523.       }
  1524.       catch(e){}
  1525.     }
  1526.   }
  1527.  
  1528.   function clickInGutter(e) {
  1529.     var cm = this, display = cm.display;
  1530.     try { var mX = e.clientX, mY = e.clientY; }
  1531.     catch(e) { return false; }
  1532.     
  1533.     if (mX >= Math.floor(display.gutters.getBoundingClientRect().right)) return false;
  1534.     e_preventDefault(e);
  1535.     if (!hasHandler(cm, "gutterClick")) return true;
  1536.  
  1537.     var lineBox = display.lineDiv.getBoundingClientRect();
  1538.     if (mY > lineBox.bottom) return true;
  1539.     mY -= lineBox.top - display.viewOffset;
  1540.  
  1541.     for (var i = 0; i < cm.options.gutters.length; ++i) {
  1542.       var g = display.gutters.childNodes[i];
  1543.       if (g && g.getBoundingClientRect().right >= mX) {
  1544.         var line = lineAtHeight(cm.view.doc, mY);
  1545.         var gutter = cm.options.gutters[i];
  1546.         signalLater(cm, cm, "gutterClick", cm, line, gutter, e);
  1547.         break;
  1548.       }
  1549.     }
  1550.     return true;
  1551.   }
  1552.  
  1553.   function onDragStart(cm, e) {
  1554.     var txt = cm.getSelection();
  1555.     e.dataTransfer.setData("Text", txt);
  1556.  
  1557.     // Use dummy image instead of default browsers image.
  1558.     if (e.dataTransfer.setDragImage)
  1559.       e.dataTransfer.setDragImage(elt('img'), 0, 0);
  1560.   }
  1561.  
  1562.   function setScrollTop(cm, val) {
  1563.     if (Math.abs(cm.view.scrollTop - val) < 2) return;
  1564.     cm.view.scrollTop = val;
  1565.     if (!gecko) updateDisplay(cm, [], val);
  1566.     if (cm.display.scroller.scrollTop != val) cm.display.scroller.scrollTop = val;
  1567.     if (cm.display.scrollbarV.scrollTop != val) cm.display.scrollbarV.scrollTop = val;
  1568.     if (gecko) updateDisplay(cm, []);
  1569.   }
  1570.   function setScrollLeft(cm, val) {
  1571.     if (Math.abs(cm.view.scrollLeft - val) < 2) return;
  1572.     cm.view.scrollLeft = val;
  1573.     if (cm.display.scroller.scrollLeft != val) cm.display.scroller.scrollLeft = val;
  1574.     if (cm.display.scrollbarH.scrollLeft != val) cm.display.scrollbarH.scrollLeft = val;
  1575.     alignVertically(cm.display);
  1576.   }
  1577.  
  1578.   // Since the delta values reported on mouse wheel events are
  1579.   // unstandardized between browsers and even browser versions, and
  1580.   // generally horribly unpredictable, this code starts by measuring
  1581.   // the scroll effect that the first few mouse wheel events have,
  1582.   // and, from that, detects the way it can convert deltas to pixel
  1583.   // offsets afterwards.
  1584.   //
  1585.   // The reason we want to know the amount a wheel event will scroll
  1586.   // is that it gives us a chance to update the display before the
  1587.   // actual scrolling happens, reducing flickering.
  1588.  
  1589.   var wheelSamples = 0, wheelDX, wheelDY, wheelStartX, wheelStartY, wheelPixelsPerUnit = null;
  1590.   // Fill in a browser-detected starting value on browsers where we
  1591.   // know one. These don't have to be accurate -- the result of them
  1592.   // being wrong would just be a slight flicker on the first wheel
  1593.   // scroll (if it is large enough).
  1594.   if (ie) wheelPixelsPerUnit = -.53;
  1595.   else if (gecko) wheelPixelsPerUnit = 15;
  1596.   else if (chrome) wheelPixelsPerUnit = -.7;
  1597.   else if (safari) wheelPixelsPerUnit = -1/3;
  1598.  
  1599.   function onScrollWheel(cm, e) {
  1600.     var dx = e.wheelDeltaX, dy = e.wheelDeltaY;
  1601.     if (dx == null && e.detail && e.axis == e.HORIZONTAL_AXIS) dx = e.detail;
  1602.     if (dy == null && e.detail && e.axis == e.VERTICAL_AXIS) dy = e.detail;
  1603.     else if (dy == null) dy = e.wheelDelta;
  1604.  
  1605.     var scroll = cm.display.scroller;
  1606.     if (wheelPixelsPerUnit != null) {
  1607.       var pixels = dy * wheelPixelsPerUnit;
  1608.       var top = cm.view.scrollTop, bot = top + cm.display.wrapper.clientHeight;
  1609.       if (pixels < 0) top = Math.max(0, top + pixels);
  1610.       else bot = Math.min(cm.view.doc.height, bot + pixels);
  1611.       updateDisplay(cm, [], {top: top, bottom: bot});
  1612.     }
  1613.     if (wheelSamples < 20) {
  1614.       if (wheelStartX == null) {
  1615.         wheelStartX = scroll.scrollLeft; wheelStartY = scroll.scrollTop;
  1616.         wheelDX = dx; wheelDY = dy;
  1617.         setTimeout(function() {
  1618.           var movedX = scroll.scrollLeft - wheelStartX;
  1619.           var movedY = scroll.scrollTop - wheelStartY;
  1620.           var sample = (movedY && wheelDY && movedY / wheelDY) ||
  1621.             (movedX && wheelDX && movedX / wheelDX);
  1622.           wheelStartX = wheelStartY = null;
  1623.           if (!sample) return;
  1624.           wheelPixelsPerUnit = (wheelPixelsPerUnit * wheelSamples + sample) / (wheelSamples + 1);
  1625.           ++wheelSamples;
  1626.         }, 200);
  1627.       } else {
  1628.         wheelDX += dx; wheelDY += dy;
  1629.       }
  1630.     }
  1631.   }
  1632.  
  1633.   function doHandleBinding(cm, bound, dropShift) {
  1634.     if (typeof bound == "string") {
  1635.       bound = commands[bound];
  1636.       if (!bound) return false;
  1637.     }
  1638.     var view = cm.view, prevShift = view.sel.shift;
  1639.     try {
  1640.       if (isReadOnly(cm)) view.suppressEdits = true;
  1641.       if (dropShift) view.sel.shift = null;
  1642.       bound(cm);
  1643.     } catch(e) {
  1644.       if (e != Pass) throw e;
  1645.       return false;
  1646.     } finally {
  1647.       view.sel.shift = prevShift;
  1648.       view.suppressEdits = false;
  1649.     }
  1650.     return true;
  1651.   }
  1652.  
  1653.   var maybeTransition;
  1654.   function handleKeyBinding(cm, e) {
  1655.     // Handle auto keymap transitions
  1656.     var startMap = getKeyMap(cm.options.keyMap), next = startMap.auto;
  1657.     clearTimeout(maybeTransition);
  1658.     if (next && !isModifierKey(e)) maybeTransition = setTimeout(function() {
  1659.       if (getKeyMap(cm.options.keyMap) == startMap)
  1660.         cm.options.keyMap = (next.call ? next.call(null, cm) : next);
  1661.     }, 50);
  1662.  
  1663.     var name = keyNames[e_prop(e, "keyCode")], handled = false;
  1664.     var flipCtrlCmd = opera && mac;
  1665.     if (name == null || e.altGraphKey) return false;
  1666.     if (e_prop(e, "altKey")) name = "Alt-" + name;
  1667.     if (e_prop(e, flipCtrlCmd ? "metaKey" : "ctrlKey")) name = "Ctrl-" + name;
  1668.     if (e_prop(e, flipCtrlCmd ? "ctrlKey" : "metaKey")) name = "Cmd-" + name;
  1669.  
  1670.     var stopped = false;
  1671.     function stop() { stopped = true; }
  1672.  
  1673.     if (e_prop(e, "shiftKey")) {
  1674.       handled = lookupKey("Shift-" + name, cm.options.extraKeys, cm.options.keyMap,
  1675.                           function(b) {return doHandleBinding(cm, b, true);}, stop)
  1676.         || lookupKey(name, cm.options.extraKeys, cm.options.keyMap, function(b) {
  1677.           if (typeof b == "string" && /^go[A-Z]/.test(b)) return doHandleBinding(cm, b);
  1678.         }, stop);
  1679.     } else {
  1680.       handled = lookupKey(name, cm.options.extraKeys, cm.options.keyMap,
  1681.                           function(b) { return doHandleBinding(cm, b); }, stop);
  1682.     }
  1683.     if (stopped) handled = false;
  1684.     if (handled) {
  1685.       e_preventDefault(e);
  1686.       restartBlink(cm);
  1687.       if (ie_lt9) { e.oldKeyCode = e.keyCode; e.keyCode = 0; }
  1688.     }
  1689.     return handled;
  1690.   }
  1691.  
  1692.   function handleCharBinding(cm, e, ch) {
  1693.     var handled = lookupKey("'" + ch + "'", cm.options.extraKeys, cm.options.keyMap,
  1694.                             function(b) { return doHandleBinding(cm, b, true); });
  1695.     if (handled) {
  1696.       e_preventDefault(e);
  1697.       restartBlink(cm);
  1698.     }
  1699.     return handled;
  1700.   }
  1701.  
  1702.   var lastStoppedKey = null;
  1703.   function onKeyDown(e) {
  1704.     var cm = this;
  1705.     if (!cm.view.focused) onFocus(cm);
  1706.     if (ie && e.keyCode == 27) { e.returnValue = false; }
  1707.     if (cm.display.pollingFast) { if (readInput(cm)) cm.display.pollingFast = false; }
  1708.     if (cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;
  1709.     var code = e_prop(e, "keyCode");
  1710.     // IE does strange things with escape.
  1711.     setShift(cm.view, code == 16 || e_prop(e, "shiftKey"));
  1712.     // First give onKeyEvent option a chance to handle this.
  1713.     var handled = handleKeyBinding(cm, e);
  1714.     if (opera) {
  1715.       lastStoppedKey = handled ? code : null;
  1716.       // Opera has no cut event... we try to at least catch the key combo
  1717.       if (!handled && code == 88 && e_prop(e, mac ? "metaKey" : "ctrlKey"))
  1718.         cm.replaceSelection("");
  1719.     }
  1720.   }
  1721.  
  1722.   function onKeyPress(e) {
  1723.     var cm = this;
  1724.     if (cm.display.pollingFast) readInput(cm);
  1725.     if (cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;
  1726.     var keyCode = e_prop(e, "keyCode"), charCode = e_prop(e, "charCode");
  1727.     if (opera && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return;}
  1728.     if (((opera && (!e.which || e.which < 10)) || khtml) && handleKeyBinding(cm, e)) return;
  1729.     var ch = String.fromCharCode(charCode == null ? keyCode : charCode);
  1730.     if (this.options.electricChars && this.view.mode.electricChars &&
  1731.         this.options.smartIndent && !isReadOnly(this) &&
  1732.         this.view.mode.electricChars.indexOf(ch) > -1)
  1733.       setTimeout(operation(cm, function() {indentLine(cm, cm.view.sel.to.line, "smart");}), 75);
  1734.     if (handleCharBinding(cm, e, ch)) return;
  1735.     fastPoll(cm);
  1736.   }
  1737.  
  1738.   function onFocus(cm) {
  1739.     if (cm.options.readOnly == "nocursor") return;
  1740.     if (!cm.view.focused) {
  1741.       signal(cm, "focus", cm);
  1742.       cm.view.focused = true;
  1743.       if (cm.display.scroller.className.search(/\bCodeMirror-focused\b/) == -1)
  1744.         cm.display.scroller.className += " CodeMirror-focused";
  1745.       resetInput(cm, true);
  1746.     }
  1747.     slowPoll(cm);
  1748.     restartBlink(cm);
  1749.   }
  1750.   function onBlur(cm) {
  1751.     if (cm.view.focused) {
  1752.       signal(cm, "blur", cm);
  1753.       cm.view.focused = false;
  1754.       cm.display.scroller.className = cm.display.scroller.className.replace(" CodeMirror-focused", "");
  1755.     }
  1756.     clearInterval(cm.display.blinker);
  1757.     setTimeout(function() {if (!cm.view.focused) cm.view.sel.shift = null;}, 150);
  1758.   }
  1759.  
  1760.   var detectingSelectAll;
  1761.   function onContextMenu(cm, e) {
  1762.     var display = cm.display, sel = cm.view.sel;
  1763.     var pos = posFromMouse(cm, e), scrollPos = display.scroller.scrollTop;
  1764.     if (!pos || opera) return; // Opera is difficult.
  1765.     if (posEq(sel.from, sel.to) || posLess(pos, sel.from) || !posLess(pos, sel.to))
  1766.       operation(cm, setSelection)(cm, pos, pos);
  1767.  
  1768.     var oldCSS = display.input.style.cssText;
  1769.     display.inputDiv.style.position = "absolute";
  1770.     display.input.style.cssText = "position: fixed; width: 30px; height: 30px; top: " + (e.clientY - 5) +
  1771.       "px; left: " + (e.clientX - 5) + "px; z-index: 1000; background: white; outline: none;" +
  1772.       "border-width: 0; outline: none; overflow: hidden; opacity: .05; filter: alpha(opacity=5);";
  1773.     focusInput(cm);
  1774.     resetInput(cm, true);
  1775.     // Adds "Select all" to context menu in FF
  1776.     if (posEq(sel.from, sel.to)) display.input.value = display.prevInput = " ";
  1777.  
  1778.     function rehide() {
  1779.       display.inputDiv.style.position = "relative";
  1780.       display.input.style.cssText = oldCSS;
  1781.       if (ie_lt9) display.scrollbarV.scrollTop = display.scroller.scrollTop = scrollPos;
  1782.       slowPoll(cm);
  1783.  
  1784.       // Try to detect the user choosing select-all 
  1785.       if (display.input.selectionStart != null) {
  1786.         clearTimeout(detectingSelectAll);
  1787.         var extval = display.input.value = " " + (posEq(sel.from, sel.to) ? "" : display.input.value), i = 0;
  1788.         display.prevInput = " ";
  1789.         display.input.selectionStart = 1; display.input.selectionEnd = extval.length;
  1790.         detectingSelectAll = setTimeout(function poll(){
  1791.           if (display.prevInput == " " && display.input.selectionStart == 0)
  1792.             operation(cm, commands.selectAll)(cm);
  1793.           else if (i++ < 10) detectingSelectAll = setTimeout(poll, 500);
  1794.           else resetInput(cm);
  1795.         }, 200);
  1796.       }
  1797.     }
  1798.  
  1799.     if (gecko) {
  1800.       e_stop(e);
  1801.       on(window, "mouseup", function mouseup() {
  1802.         off(window, "mouseup", mouseup);
  1803.         setTimeout(rehide, 20);
  1804.       });
  1805.     } else {
  1806.       setTimeout(rehide, 50);
  1807.     }
  1808.   }
  1809.  
  1810.   // UPDATING
  1811.  
  1812.   // Replace the range from from to to by the strings in newText.
  1813.   // Afterwards, set the selection to selFrom, selTo.
  1814.   function updateDoc(cm, from, to, newText, selUpdate) {
  1815.     // Possibly split or suppress the update based on the presence
  1816.     // of read-only spans in its range.
  1817.     var split = sawReadOnlySpans &&
  1818.       removeReadOnlyRanges(cm.view.doc, from, to);
  1819.     if (split) {
  1820.       for (var i = split.length - 1; i >= 1; --i)
  1821.         updateDocInner(cm, split[i].from, split[i].to, [""]);
  1822.       if (split.length)
  1823.         return updateDocInner(cm, split[0].from, split[0].to, newText, selUpdate);
  1824.     } else {
  1825.       return updateDocInner(cm, from, to, newText, selUpdate);
  1826.     }
  1827.   }
  1828.  
  1829.   function updateDocInner(cm, from, to, newText, selUpdate) {
  1830.     if (cm.view.suppressEdits) return;
  1831.  
  1832.     var view = cm.view, doc = view.doc, old = [];
  1833.     doc.iter(from.line, to.line + 1, function(line) {
  1834.       old.push(newHL(line.text, line.markedSpans));
  1835.     });
  1836.     if (view.history) {
  1837.       addChange(view.history, from.line, newText.length, old);
  1838.       while (view.history.done.length > cm.options.undoDepth) view.history.done.shift();
  1839.     }
  1840.     var lines = updateMarkedSpans(hlSpans(old[0]), hlSpans(lst(old)), from.ch, to.ch, newText);
  1841.     return updateDocNoUndo(cm, from, to, lines, selUpdate);
  1842.   }
  1843.  
  1844.   function unredoHelper(cm, from, to, dir) {
  1845.     var doc = cm.view.doc, hist = cm.view.history;
  1846.     if (!from.length) return;
  1847.     var set = from.pop(), out = [];
  1848.     for (var i = set.length - 1; i >= 0; i -= 1) {
  1849.       hist.dirtyCounter += dir;
  1850.       var change = set[i];
  1851.       var replaced = [], end = change.start + change.added;
  1852.       doc.iter(change.start, end, function(line) { replaced.push(newHL(line.text, line.markedSpans)); });
  1853.       out.push({start: change.start, added: change.old.length, old: replaced});
  1854.       var pos = {line: change.start + change.old.length - 1,
  1855.                  ch: editEnd(hlText(lst(replaced)), hlText(lst(change.old)))};
  1856.       updateDocNoUndo(cm, {line: change.start, ch: 0}, {line: end - 1, ch: getLine(doc, end-1).text.length},
  1857.                       change.old, pos);
  1858.     }
  1859.     to.push(out);
  1860.   }
  1861.  
  1862.   function updateDocNoUndo(cm, from, to, lines, selUpdate) {
  1863.     var view = cm.view, doc = view.doc, display = cm.display;
  1864.     if (view.suppressEdits) return;
  1865.  
  1866.     var nlines = to.line - from.line, firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);
  1867.     var recomputeMaxLength = false, checkWidthStart = from.line;
  1868.     if (!cm.options.lineWrapping) {
  1869.       for (var cur = firstLine, merged; merged = collapsedSpanAtStart(cur);) {
  1870.         checkWidthStart = merged.find().from.line;
  1871.         cur = getLine(doc, checkWidthStart);
  1872.       }
  1873.       doc.iter(checkWidthStart, to.line + 1, function(line) {
  1874.         if (lineLength(doc, line) == view.maxLineLength) {
  1875.           recomputeMaxLength = true;
  1876.           return true;
  1877.         }
  1878.       });
  1879.     }
  1880.  
  1881.     var lastHL = lst(lines), th = textHeight(display);
  1882.  
  1883.     // First adjust the line structure
  1884.     if (from.ch == 0 && to.ch == 0 && hlText(lastHL) == "") {
  1885.       // This is a whole-line replace. Treated specially to make
  1886.       // sure line objects move the way they are supposed to.
  1887.       var added = [], prevLine = null;
  1888.       for (var i = 0, e = lines.length - 1; i < e; ++i)
  1889.         added.push(makeLine(hlText(lines[i]), hlSpans(lines[i]), th));
  1890.       updateLine(cm, lastLine, lastLine.text, hlSpans(lastHL));
  1891.       if (nlines) doc.remove(from.line, nlines, cm);
  1892.       if (added.length) doc.insert(from.line, added);
  1893.     } else if (firstLine == lastLine) {
  1894.       if (lines.length == 1) {
  1895.         updateLine(cm, firstLine, firstLine.text.slice(0, from.ch) + hlText(lines[0]) +
  1896.                    firstLine.text.slice(to.ch), hlSpans(lines[0]));
  1897.       } else {
  1898.         for (var added = [], i = 1, e = lines.length - 1; i < e; ++i)
  1899.           added.push(makeLine(hlText(lines[i]), hlSpans(lines[i]), th));
  1900.         added.push(makeLine(hlText(lastHL) + firstLine.text.slice(to.ch), hlSpans(lastHL), th));
  1901.         updateLine(cm, firstLine, firstLine.text.slice(0, from.ch) + hlText(lines[0]), hlSpans(lines[0]));
  1902.         doc.insert(from.line + 1, added);
  1903.       }
  1904.     } else if (lines.length == 1) {
  1905.       updateLine(cm, firstLine, firstLine.text.slice(0, from.ch) + hlText(lines[0]) +
  1906.                  lastLine.text.slice(to.ch), hlSpans(lines[0]));
  1907.       doc.remove(from.line + 1, nlines, cm);
  1908.     } else {
  1909.       var added = [];
  1910.       updateLine(cm, firstLine, firstLine.text.slice(0, from.ch) + hlText(lines[0]), hlSpans(lines[0]));
  1911.       updateLine(cm, lastLine, hlText(lastHL) + lastLine.text.slice(to.ch), hlSpans(lastHL));
  1912.       for (var i = 1, e = lines.length - 1; i < e; ++i)
  1913.         added.push(makeLine(hlText(lines[i]), hlSpans(lines[i]), th));
  1914.       if (nlines > 1) doc.remove(from.line + 1, nlines - 1, cm);
  1915.       doc.insert(from.line + 1, added);
  1916.     }
  1917.  
  1918.     if (cm.options.lineWrapping) {
  1919.       var perLine = Math.max(5, display.scroller.clientWidth / charWidth(display) - 3);
  1920.       doc.iter(from.line, from.line + lines.length, function(line) {
  1921.         if (line.height == 0) return;
  1922.         var guess = (Math.ceil(line.text.length / perLine) || 1) * th;
  1923.         if (guess != line.height) updateLineHeight(line, guess);
  1924.       });
  1925.     } else {
  1926.       doc.iter(checkWidthStart, from.line + lines.length, function(line) {
  1927.         var len = lineLength(doc, line);
  1928.         if (len > view.maxLineLength) {
  1929.           view.maxLine = line;
  1930.           view.maxLineLength = len;
  1931.           view.maxLineChanged = true;
  1932.           recomputeMaxLength = false;
  1933.         }
  1934.       });
  1935.       if (recomputeMaxLength) cm.curOp.updateMaxLine = true;
  1936.     }
  1937.  
  1938.     // Adjust frontier, schedule worker
  1939.     view.frontier = Math.min(view.frontier, from.line);
  1940.     startWorker(cm, 400);
  1941.  
  1942.     var lendiff = lines.length - nlines - 1;
  1943.     // Remember that these lines changed, for updating the display
  1944.     regChange(cm, from.line, to.line + 1, lendiff);
  1945.     if (hasHandler(cm, "change")) {
  1946.       // Normalize lines to contain only strings, since that's what
  1947.       // the change event handler expects
  1948.       for (var i = 0; i < lines.length; ++i)
  1949.         if (typeof lines[i] != "string") lines[i] = lines[i].text;
  1950.       var changeObj = {from: from, to: to, text: lines};
  1951.       if (cm.curOp.textChanged) {
  1952.         for (var cur = cm.curOp.textChanged; cur.next; cur = cur.next) {}
  1953.         cur.next = changeObj;
  1954.       } else cm.curOp.textChanged = changeObj;
  1955.     }
  1956.  
  1957.     // Update the selection
  1958.     var newSelFrom, newSelTo, end = {line: from.line + lines.length - 1,
  1959.                                      ch: hlText(lastHL).length  + (lines.length == 1 ? from.ch : 0)};
  1960.     if (typeof selUpdate == "object" && selUpdate.line != null) {
  1961.       newSelFrom = newSelTo = selUpdate;
  1962.     } else if (selUpdate == "end") {
  1963.       newSelFrom = newSelTo = end;
  1964.     } else if (selUpdate == "start") {
  1965.       newSelFrom = newSelTo = from;
  1966.     } else if (selUpdate == "around") {
  1967.       newSelFrom = from; newSelTo = end;
  1968.     } else {
  1969.       var adjustPos = function(pos) {
  1970.         if (posLess(pos, from)) return pos;
  1971.         if (!posLess(to, pos)) return end;
  1972.         var line = pos.line + lendiff;
  1973.         var ch = pos.ch;
  1974.         if (pos.line == to.line)
  1975.           ch += hlText(lastHL).length - (to.ch - (to.line == from.line ? from.ch : 0));
  1976.         return {line: line, ch: ch};
  1977.       };
  1978.       newSelFrom = adjustPos(view.sel.from);
  1979.       newSelTo = adjustPos(view.sel.to);
  1980.     }
  1981.     setSelection(cm, newSelFrom, newSelTo, null, true);
  1982.     return end;
  1983.   }
  1984.  
  1985.   function replaceRange(cm, code, from, to) {
  1986.     if (!to) to = from;
  1987.     if (posLess(to, from)) { var tmp = to; to = from; from = tmp; }
  1988.     code = splitLines(code);
  1989.     function adjustPos(pos) {
  1990.       if (posLess(pos, from)) return pos;
  1991.       if (!posLess(to, pos)) return end;
  1992.       var line = pos.line + code.length - (to.line - from.line) - 1;
  1993.       var ch = pos.ch;
  1994.       if (pos.line == to.line)
  1995.         ch += lst(code).length - (to.ch - (to.line == from.line ? from.ch : 0));
  1996.       return {line: line, ch: ch};
  1997.     }
  1998.     return updateDoc(cm, from, to, code);
  1999.   }
  2000.  
  2001.   // SELECTION
  2002.  
  2003.   function posEq(a, b) {return a.line == b.line && a.ch == b.ch;}
  2004.   function posLess(a, b) {return a.line < b.line || (a.line == b.line && a.ch < b.ch);}
  2005.   function copyPos(x) {return {line: x.line, ch: x.ch};}
  2006.  
  2007.   function clipLine(doc, n) {return Math.max(0, Math.min(n, doc.size-1));}
  2008.   function clipPos(doc, pos) {
  2009.     if (pos.line < 0) return {line: 0, ch: 0};
  2010.     if (pos.line >= doc.size) return {line: doc.size-1, ch: getLine(doc, doc.size-1).text.length};
  2011.     var ch = pos.ch, linelen = getLine(doc, pos.line).text.length;
  2012.     if (ch == null || ch > linelen) return {line: pos.line, ch: linelen};
  2013.     else if (ch < 0) return {line: pos.line, ch: 0};
  2014.     else return pos;
  2015.   }
  2016.   function isLine(doc, l) {return l >= 0 && l < doc.size;}
  2017.  
  2018.   function setShift(view, val) {
  2019.     if (val) view.sel.shift = view.sel.shift || selHead(view);
  2020.     else view.sel.shift = null;
  2021.   }
  2022.   function setSelectionUser(cm, from, to, bias) {
  2023.     var view = cm.view, sh = view.sel.shift;
  2024.     if (sh) {
  2025.       sh = clipPos(view.doc, sh);
  2026.       if (posLess(sh, from)) from = sh;
  2027.       else if (posLess(to, sh)) to = sh;
  2028.     }
  2029.     setSelection(cm, from, to, bias);
  2030.     cm.curOp.userSelChange = true;
  2031.   }
  2032.  
  2033.   // Update the selection. Last two args are only used by
  2034.   // updateDoc, since they have to be expressed in the line
  2035.   // numbers before the update.
  2036.   function setSelection(cm, from, to, bias, checkAtomic) {
  2037.     cm.curOp.updateInput = true;
  2038.     var sel = cm.view.sel, doc = cm.view.doc;
  2039.     cm.view.goalColumn = null;
  2040.     if (!checkAtomic && posEq(sel.from, from) && posEq(sel.to, to)) return;
  2041.     if (posLess(to, from)) {var tmp = to; to = from; from = tmp;}
  2042.  
  2043.     // Skip over atomic spans.
  2044.     if (checkAtomic || !posEq(from, sel.from))
  2045.       from = skipAtomic(cm, from, bias, checkAtomic != "push");
  2046.     if (checkAtomic || !posEq(to, sel.to))
  2047.       to = skipAtomic(cm, to, bias, checkAtomic != "push");
  2048.  
  2049.     if (posEq(from, to)) sel.inverted = false;
  2050.     else if (posEq(from, sel.to)) sel.inverted = false;
  2051.     else if (posEq(to, sel.from)) sel.inverted = true;
  2052.  
  2053.     if (cm.options.autoClearEmptyLines && posEq(sel.from, sel.to)) {
  2054.       var head = selHead(cm.view);
  2055.       if (head.line != sel.from.line && sel.from.line < doc.size) {
  2056.         var oldLine = getLine(doc, sel.from.line);
  2057.         if (/^\s+$/.test(oldLine.text))
  2058.           setTimeout(operation(cm, function() {
  2059.             if (oldLine.parent && /^\s+$/.test(oldLine.text)) {
  2060.               var no = lineNo(oldLine);
  2061.               replaceRange(cm, "", {line: no, ch: 0}, {line: no, ch: oldLine.text.length});
  2062.             }
  2063.           }, 10));
  2064.       }
  2065.     }
  2066.  
  2067.     sel.from = from; sel.to = to;
  2068.     cm.curOp.selectionChanged = true;
  2069.   }
  2070.  
  2071.   function reCheckSelection(cm) {
  2072.     setSelection(cm, cm.view.sel.from, cm.view.sel.to, null, "push");
  2073.   }
  2074.  
  2075.   function skipAtomic(cm, pos, bias, mayClear) {
  2076.     var doc = cm.view.doc, flipped = false, curPos = pos;
  2077.     var dir = bias || 1;
  2078.     cm.view.cantEdit = false;
  2079.     search: for (;;) {
  2080.       var line = getLine(doc, curPos.line), toClear;
  2081.       if (line.markedSpans) {
  2082.         for (var i = 0; i < line.markedSpans.length; ++i) {
  2083.           var sp = line.markedSpans[i], m = sp.marker;
  2084.           if ((sp.from == null || (m.inclusiveLeft ? sp.from <= curPos.ch : sp.from < curPos.ch)) &&
  2085.               (sp.to == null || (m.inclusiveRight ? sp.to >= curPos.ch : sp.to > curPos.ch))) {
  2086.             if (mayClear && m.clearOnEnter) {
  2087.               (toClear || (toClear = [])).push(m);
  2088.               continue;
  2089.             } else if (!m.atomic) continue;
  2090.             var newPos = m.find()[dir < 0 ? "from" : "to"];
  2091.             if (posEq(newPos, curPos)) {
  2092.               newPos.ch += dir;
  2093.               if (newPos.ch < 0) {
  2094.                 if (newPos.line) newPos = clipPos(doc, {line: newPos.line - 1});
  2095.                 else newPos = null;
  2096.               } else if (newPos.ch > line.text.length) {
  2097.                 if (newPos.line < doc.size - 1) newPos = {line: newPos.line + 1, ch: 0};
  2098.                 else newPos = null;
  2099.               }
  2100.               if (!newPos) {
  2101.                 if (flipped) {
  2102.                   // Driven in a corner -- no valid cursor position found at all
  2103.                   // -- try again *with* clearing, if we didn't already
  2104.                   if (!mayClear) return skipAtomic(cm, pos, bias, true);
  2105.                   // Otherwise, turn off editing until further notice, and return the start of the doc
  2106.                   cm.view.cantEdit = true;
  2107.                   return {line: 0, ch: 0};
  2108.                 }
  2109.                 flipped = true; newPos = pos; dir = -dir;
  2110.               }
  2111.             }
  2112.             curPos = newPos;
  2113.             continue search;
  2114.           }
  2115.         }
  2116.         if (toClear) for (var i = 0; i < toClear.length; ++i) toClear[i].clear();
  2117.       }
  2118.       return curPos;
  2119.     }
  2120.   }
  2121.  
  2122.   // SCROLLING
  2123.  
  2124.   function scrollCursorIntoView(cm) {
  2125.     var view = cm.view, coords = cursorCoords(cm, selHead(view));
  2126.     scrollIntoView(cm.display, coords.left, coords.top, coords.left, coords.bottom);
  2127.     if (!view.focused) return;
  2128.     var display = cm.display, box = display.sizer.getBoundingClientRect(), doScroll = null;
  2129.     if (coords.top + box.top < 0) doScroll = true;
  2130.     else if (coords.bottom + box.top > (window.innerHeight || document.documentElement.clientHeight)) doScroll = false;
  2131.     if (doScroll != null && !phantom) {
  2132.       var hidden = display.cursor.style.display == "none";
  2133.       if (hidden) {
  2134.         display.cursor.style.display = "";
  2135.         display.cursor.style.left = coords.left + "px";
  2136.         display.cursor.style.top = (coords.top - display.viewOffset) + "px";
  2137.       }
  2138.       display.cursor.scrollIntoView(doScroll);
  2139.       if (hidden) display.cursor.style.display = "none";
  2140.     }
  2141.   }
  2142.  
  2143.   function scrollIntoView(display, x1, y1, x2, y2) {
  2144.     var scrollPos = calculateScrollPos(display, x1, y1, x2, y2);
  2145.     if (scrollPos.scrollLeft != null) {display.scrollbarH.scrollLeft = display.scroller.scrollLeft = scrollPos.scrollLeft;}
  2146.     if (scrollPos.scrollTop != null) {display.scrollbarV.scrollTop = display.scroller.scrollTop = scrollPos.scrollTop;}
  2147.   }
  2148.  
  2149.   function calculateScrollPos(display, x1, y1, x2, y2) {
  2150.     var pt = paddingTop(display);
  2151.     y1 += pt; y2 += pt;
  2152.     var screen = display.scroller.clientHeight - scrollerCutOff, screentop = display.scroller.scrollTop, result = {};
  2153.     var docBottom = display.scroller.scrollHeight - scrollerCutOff;
  2154.     var atTop = y1 < pt + 10, atBottom = y2 + pt > docBottom - 10;
  2155.     if (y1 < screentop) result.scrollTop = atTop ? 0 : Math.max(0, y1);
  2156.     else if (y2 > screentop + screen) result.scrollTop = (atBottom ? docBottom : y2 - screen);
  2157.  
  2158.     var screenw = display.scroller.clientWidth - scrollerCutOff, screenleft = display.scroller.scrollLeft;
  2159.     x1 += display.gutters.offsetWidth; x2 += display.gutters.offsetWidth;
  2160.     var gutterw = display.gutters.offsetWidth;
  2161.     var atLeft = x1 < gutterw + 10;
  2162.     if (x1 < screenleft + gutterw || atLeft) {
  2163.       if (atLeft) x1 = 0;
  2164.       result.scrollLeft = Math.max(0, x1 - 10 - gutterw);
  2165.     } else if (x2 > screenw + screenleft - 3) {
  2166.       result.scrollLeft = x2 + 10 - screenw;
  2167.     }
  2168.     return result;
  2169.   }
  2170.  
  2171.   // API UTILITIES
  2172.  
  2173.   function indentLine(cm, n, how) {
  2174.     var doc = cm.view.doc;
  2175.     if (!how) how = "add";
  2176.     if (how == "smart") {
  2177.       if (!cm.view.mode.indent) how = "prev";
  2178.       else var state = getStateBefore(cm, n);
  2179.     }
  2180.  
  2181.     var tabSize = cm.options.tabSize;
  2182.     var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize);
  2183.     var curSpaceString = line.text.match(/^\s*/)[0], indentation;
  2184.     if (how == "smart") {
  2185.       indentation = cm.view.mode.indent(state, line.text.slice(curSpaceString.length), line.text);
  2186.       if (indentation == Pass) how = "prev";
  2187.     }
  2188.     if (how == "prev") {
  2189.       if (n) indentation = countColumn(getLine(doc, n-1).text, null, tabSize);
  2190.       else indentation = 0;
  2191.     }
  2192.     else if (how == "add") indentation = curSpace + cm.options.indentUnit;
  2193.     else if (how == "subtract") indentation = curSpace - cm.options.indentUnit;
  2194.     indentation = Math.max(0, indentation);
  2195.     var diff = indentation - curSpace;
  2196.  
  2197.     var indentString = "", pos = 0;
  2198.     if (cm.options.indentWithTabs)
  2199.       for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";}
  2200.     if (pos < indentation) indentString += spaceStr(indentation - pos);
  2201.  
  2202.     if (indentString != curSpaceString)
  2203.       replaceRange(cm, indentString, {line: n, ch: 0}, {line: n, ch: curSpaceString.length});
  2204.     line.stateAfter = null;
  2205.   }
  2206.  
  2207.   function changeLine(cm, handle, op) {
  2208.     var no = handle, line = handle, doc = cm.view.doc;
  2209.     if (typeof handle == "number") line = getLine(doc, clipLine(doc, handle));
  2210.     else no = lineNo(handle);
  2211.     if (no == null) return null;
  2212.     if (op(line, no)) regChange(cm, no, no + 1);
  2213.     else return null;
  2214.     return line;
  2215.   }
  2216.  
  2217.   function findPosH(cm, dir, unit, visually) {
  2218.     var doc = cm.view.doc, end = selHead(cm.view), line = end.line, ch = end.ch;
  2219.     var lineObj = getLine(doc, line);
  2220.     function findNextLine() {
  2221.       var l = line + dir;
  2222.       if (l < 0 || l == doc.size) return false;
  2223.       line = l;
  2224.       return lineObj = getLine(doc, l);
  2225.     }
  2226.     function moveOnce(boundToLine) {
  2227.       var next = (visually ? moveVisually : moveLogically)(lineObj, ch, dir, true);
  2228.       if (next == null) {
  2229.         if (!boundToLine && findNextLine()) {
  2230.           if (visually) ch = (dir < 0 ? lineRight : lineLeft)(lineObj);
  2231.           else ch = dir < 0 ? lineObj.text.length : 0;
  2232.         } else return false;
  2233.       } else ch = next;
  2234.       return true;
  2235.     }
  2236.     if (unit == "char") moveOnce();
  2237.     else if (unit == "column") moveOnce(true);
  2238.     else if (unit == "word") {
  2239.       var sawWord = false;
  2240.       for (;;) {
  2241.         if (dir < 0) if (!moveOnce()) break;
  2242.         if (isWordChar(lineObj.text.charAt(ch))) sawWord = true;
  2243.         else if (sawWord) {if (dir < 0) {dir = 1; moveOnce();} break;}
  2244.         if (dir > 0) if (!moveOnce()) break;
  2245.       }
  2246.     }
  2247.     return skipAtomic(cm, {line: line, ch: ch}, dir, true);
  2248.   }
  2249.  
  2250.   function findWordAt(line, pos) {
  2251.     var start = pos.ch, end = pos.ch;
  2252.     if (line) {
  2253.       if (pos.after === false || end == line.length) --start; else ++end;
  2254.       var startChar = line.charAt(start);
  2255.       var check = isWordChar(startChar) ? isWordChar :
  2256.         /\s/.test(startChar) ? function(ch) {return /\s/.test(ch);} :
  2257.       function(ch) {return !/\s/.test(ch) && !isWordChar(ch);};
  2258.       while (start > 0 && check(line.charAt(start - 1))) --start;
  2259.       while (end < line.length && check(line.charAt(end))) ++end;
  2260.     }
  2261.     return {from: {line: pos.line, ch: start}, to: {line: pos.line, ch: end}};
  2262.   }
  2263.  
  2264.   function selectLine(cm, line) {
  2265.     setSelectionUser(cm, {line: line, ch: 0}, clipPos(cm.view.doc, {line: line + 1, ch: 0}));
  2266.   }
  2267.  
  2268.   // PROTOTYPE
  2269.  
  2270.   // The publicly visible API. Note that operation(null, f) means
  2271.   // 'wrap f in an operation, performed on its `this` parameter'
  2272.  
  2273.   CodeMirror.prototype = {
  2274.     getValue: function(lineSep) {
  2275.       var text = [], doc = this.view.doc;
  2276.       doc.iter(0, doc.size, function(line) { text.push(line.text); });
  2277.       return text.join(lineSep || "\n");
  2278.     },
  2279.  
  2280.     setValue: operation(null, function(code) {
  2281.       var doc = this.view.doc, top = {line: 0, ch: 0}, lastLen = getLine(doc, doc.size-1).text.length;
  2282.       updateDocInner(this, top, {line: doc.size - 1, ch: lastLen}, splitLines(code), top, top);
  2283.     }),
  2284.  
  2285.     getSelection: function(lineSep) { return this.getRange(this.view.sel.from, this.view.sel.to, lineSep); },
  2286.  
  2287.     replaceSelection: operation(null, function(code, collapse) {
  2288.       var sel = this.view.sel;
  2289.       updateDoc(this, sel.from, sel.to, splitLines(code), collapse || "around");
  2290.     }),
  2291.  
  2292.     focus: function(){window.focus(); focusInput(this); onFocus(this); fastPoll(this);},
  2293.  
  2294.     setOption: function(option, value) {
  2295.       var options = this.options, old = options[option];
  2296.       if (options[option] == value && option != "mode") return;
  2297.       options[option] = value;
  2298.       if (optionHandlers.hasOwnProperty(option))
  2299.         operation(this, optionHandlers[option])(this, value, old);
  2300.     },
  2301.  
  2302.     getOption: function(option) {return this.options[option];},
  2303.  
  2304.     getMode: function() {return this.view.mode;},
  2305.  
  2306.     undo: operation(null, function() {
  2307.       var hist = this.view.history;
  2308.       unredoHelper(this, hist.done, hist.undone, -1);
  2309.     }),
  2310.     redo: operation(null, function() {
  2311.       var hist = this.view.history;
  2312.       unredoHelper(this, hist.undone, hist.done, 1);
  2313.     }),
  2314.  
  2315.     indentLine: operation(null, function(n, dir) {
  2316.       if (typeof dir != "string") {
  2317.         if (dir == null) dir = this.options.smartIndent ? "smart" : "prev";
  2318.         else dir = dir ? "add" : "subtract";
  2319.       }
  2320.       if (isLine(this.view.doc, n)) indentLine(this, n, dir);
  2321.     }),
  2322.  
  2323.     indentSelection: operation(null, function(how) {
  2324.       var sel = this.view.sel;
  2325.       if (posEq(sel.from, sel.to)) return indentLine(this, sel.from.line, how);
  2326.       var e = sel.to.line - (sel.to.ch ? 0 : 1);
  2327.       for (var i = sel.from.line; i <= e; ++i) indentLine(this, i, how);
  2328.     }),
  2329.  
  2330.     historySize: function() {
  2331.       var hist = this.view.history;
  2332.       return {undo: hist.done.length, redo: hist.undone.length};
  2333.     },
  2334.  
  2335.     clearHistory: function() {this.view.history = makeHistory();},
  2336.  
  2337.     markClean: function() {
  2338.       this.view.history.dirtyCounter = 0;
  2339.       this.view.history.closed = true;
  2340.     },
  2341.  
  2342.     isClean: function () {return this.view.history.dirtyCounter == 0;},
  2343.       
  2344.     getHistory: function() {
  2345.       var hist = this.view.history;
  2346.       function cp(arr) {
  2347.         for (var i = 0, nw = [], nwelt; i < arr.length; ++i) {
  2348.           nw.push(nwelt = []);
  2349.           for (var j = 0, elt = arr[i]; j < elt.length; ++j) {
  2350.             var old = [], cur = elt[j];
  2351.             nwelt.push({start: cur.start, added: cur.added, old: old});
  2352.             for (var k = 0; k < cur.old.length; ++k) old.push(hlText(cur.old[k]));
  2353.           }
  2354.         }
  2355.         return nw;
  2356.       }
  2357.       return {done: cp(hist.done), undone: cp(hist.undone)};
  2358.     },
  2359.  
  2360.     setHistory: function(histData) {
  2361.       var hist = this.view.history = makeHistory();
  2362.       hist.done = histData.done;
  2363.       hist.undone = histData.undone;
  2364.     },
  2365.  
  2366.     getTokenAt: function(pos) {
  2367.       var doc = this.view.doc;
  2368.       pos = clipPos(doc, pos);
  2369.       return getTokenAt(this, getLine(doc, pos.line), getStateBefore(this, pos.line), pos.ch);
  2370.     },
  2371.  
  2372.     getStateAfter: function(line) {
  2373.       var doc = this.view.doc;
  2374.       line = clipLine(doc, line == null ? doc.size - 1: line);
  2375.       return getStateBefore(this, line + 1);
  2376.     },
  2377.  
  2378.     cursorCoords: function(start, mode) {
  2379.       var pos, sel = this.view.sel;
  2380.       if (start == null) start = sel.inverted;
  2381.       if (typeof start == "object") pos = clipPos(this.view.doc, start);
  2382.       else pos = start ? sel.from : sel.to;
  2383.       return cursorCoords(this, pos, mode || "page");
  2384.     },
  2385.  
  2386.     charCoords: function(pos, mode) {
  2387.       return charCoords(this, clipPos(this.view.doc, pos), mode || "page");
  2388.     },
  2389.  
  2390.     coordsChar: function(coords) {
  2391.       var off = this.display.lineSpace.getBoundingClientRect();
  2392.       return coordsChar(this, coords.left - off.left, coords.top - off.top);
  2393.     },
  2394.  
  2395.     defaultTextHeight: function() { return textHeight(this.display); },
  2396.  
  2397.     markText: operation(null, function(from, to, options) {
  2398.       return markText(this, clipPos(this.view.doc, from), clipPos(this.view.doc, to),
  2399.                       options, "range");
  2400.     }),
  2401.  
  2402.     setBookmark: operation(null, function(pos, widget) {
  2403.       pos = clipPos(this.view.doc, pos);
  2404.       return markText(this, pos, pos, widget ? {replacedWith: widget} : {}, "bookmark");
  2405.     }),
  2406.  
  2407.     findMarksAt: function(pos) {
  2408.       var doc = this.view.doc;
  2409.       pos = clipPos(doc, pos);
  2410.       var markers = [], spans = getLine(doc, pos.line).markedSpans;
  2411.       if (spans) for (var i = 0; i < spans.length; ++i) {
  2412.         var span = spans[i];
  2413.         if ((span.from == null || span.from <= pos.ch) &&
  2414.             (span.to == null || span.to >= pos.ch))
  2415.           markers.push(span.marker);
  2416.       }
  2417.       return markers;
  2418.     },
  2419.  
  2420.     setGutterMarker: operation(null, function(line, gutterID, value) {
  2421.       return changeLine(this, line, function(line) {
  2422.         var markers = line.gutterMarkers || (line.gutterMarkers = {});
  2423.         markers[gutterID] = value;
  2424.         if (!value && isEmpty(markers)) line.gutterMarkers = null;
  2425.         return true;
  2426.       });
  2427.     }),
  2428.  
  2429.     clearGutter: operation(null, function(gutterID) {
  2430.       var i = 0, cm = this, doc = cm.view.doc;
  2431.       doc.iter(0, doc.size, function(line) {
  2432.         if (line.gutterMarkers && line.gutterMarkers[gutterID]) {
  2433.           line.gutterMarkers[gutterID] = null;
  2434.           regChange(cm, i, i + 1);
  2435.           if (isEmpty(line.gutterMarkers)) line.gutterMarkers = null;
  2436.         }
  2437.         ++i;
  2438.       });
  2439.     }),
  2440.  
  2441.     addLineClass: operation(null, function(handle, where, cls) {
  2442.       return changeLine(this, handle, function(line) {
  2443.         var prop = where == "text" ? "textClass" : where == "background" ? "bgClass" : "wrapClass";
  2444.         if (!line[prop]) line[prop] = cls;
  2445.         else if (new RegExp("\\b" + cls + "\\b").test(line[prop])) return false;
  2446.         else line[prop] += " " + cls;
  2447.         return true;
  2448.       });
  2449.     }),
  2450.  
  2451.     removeLineClass: operation(null, function(handle, where, cls) {
  2452.       return changeLine(this, handle, function(line) {
  2453.         var prop = where == "text" ? "textClass" : where == "background" ? "bgClass" : "wrapClass";
  2454.         var cur = line[prop];
  2455.         if (!cur) return false;
  2456.         else if (cls == null) line[prop] = null;
  2457.         else {
  2458.           var upd = cur.replace(new RegExp("^" + cls + "\\b\\s*|\\s*\\b" + cls + "\\b"), "");
  2459.           if (upd == cur) return false;
  2460.           line[prop] = upd || null;
  2461.         }
  2462.         return true;
  2463.       });
  2464.     }),
  2465.  
  2466.     addLineWidget: operation(null, function addLineWidget(handle, node, options) {
  2467.       var widget = options || {};
  2468.       widget.node = node;
  2469.       if (widget.noHScroll) this.display.alignWidgets = true;
  2470.       changeLine(this, handle, function(line) {
  2471.         (line.widgets || (line.widgets = [])).push(widget);
  2472.         widget.line = line;
  2473.         return true;
  2474.       });
  2475.       return widget;
  2476.     }),
  2477.  
  2478.     removeLineWidget: operation(null, function(widget) {
  2479.       var ws = widget.line.widgets, no = lineNo(widget.line);
  2480.       if (no == null) return;
  2481.       for (var i = 0; i < ws.length; ++i) if (ws[i] == widget) ws.splice(i--, 1);
  2482.       regChange(this, no, no + 1);
  2483.     }),
  2484.  
  2485.     lineInfo: function(line) {
  2486.       if (typeof line == "number") {
  2487.         if (!isLine(this.view.doc, line)) return null;
  2488.         var n = line;
  2489.         line = getLine(this.view.doc, line);
  2490.         if (!line) return null;
  2491.       } else {
  2492.         var n = lineNo(line);
  2493.         if (n == null) return null;
  2494.       }
  2495.       return {line: n, handle: line, text: line.text, gutterMarkers: line.gutterMarkers,
  2496.               textClass: line.textClass, bgClass: line.bgClass, wrapClass: line.wrapClass,
  2497.               widgets: line.widgets};
  2498.     },
  2499.  
  2500.     getViewport: function() { return {from: this.display.showingFrom, to: this.display.showingTo};},
  2501.  
  2502.     addWidget: function(pos, node, scroll, vert, horiz) {
  2503.       var display = this.display;
  2504.       pos = cursorCoords(this, clipPos(this.view.doc, pos));
  2505.       var top = pos.top, left = pos.left;
  2506.       node.style.position = "absolute";
  2507.       display.sizer.appendChild(node);
  2508.       if (vert == "over") top = pos.top;
  2509.       else if (vert == "near") {
  2510.         var vspace = Math.max(display.wrapper.clientHeight, this.view.doc.height),
  2511.         hspace = Math.max(display.sizer.clientWidth, display.lineSpace.clientWidth);
  2512.         if (pos.bottom + node.offsetHeight > vspace && pos.top > node.offsetHeight)
  2513.           top = pos.top - node.offsetHeight;
  2514.         if (left + node.offsetWidth > hspace)
  2515.           left = hspace - node.offsetWidth;
  2516.       }
  2517.       node.style.top = (top + paddingTop(display)) + "px";
  2518.       node.style.left = node.style.right = "";
  2519.       if (horiz == "right") {
  2520.         left = display.sizer.clientWidth - node.offsetWidth;
  2521.         node.style.right = "0px";
  2522.       } else {
  2523.         if (horiz == "left") left = 0;
  2524.         else if (horiz == "middle") left = (display.sizer.clientWidth - node.offsetWidth) / 2;
  2525.         node.style.left = left + "px";
  2526.       }
  2527.       if (scroll)
  2528.         scrollIntoView(display, left, top, left + node.offsetWidth, top + node.offsetHeight);
  2529.     },
  2530.  
  2531.     lineCount: function() {return this.view.doc.size;},
  2532.  
  2533.     clipPos: function(pos) {return clipPos(this.view.doc, pos);},
  2534.  
  2535.     getCursor: function(start) {
  2536.       var sel = this.view.sel;
  2537.       if (start == null) start = sel.inverted;
  2538.       return copyPos(start ? sel.from : sel.to);
  2539.     },
  2540.  
  2541.     somethingSelected: function() {return !posEq(this.view.sel.from, this.view.sel.to);},
  2542.  
  2543.     setCursor: operation(null, function(line, ch, user) {
  2544.       var pos = typeof line == "number" ? {line: line, ch: ch || 0} : line;
  2545.       (user ? setSelectionUser : setSelection)(this, pos, pos);
  2546.     }),
  2547.  
  2548.     setSelection: operation(null, function(from, to, user) {
  2549.       var doc = this.view.doc;
  2550.       (user ? setSelectionUser : setSelection)(this, clipPos(doc, from), clipPos(doc, to || from));
  2551.     }),
  2552.  
  2553.     getLine: function(line) {var l = this.getLineHandle(line); return l && l.text;},
  2554.  
  2555.     getLineHandle: function(line) {
  2556.       var doc = this.view.doc;
  2557.       if (isLine(doc, line)) return getLine(doc, line);
  2558.     },
  2559.  
  2560.     getLineNumber: function(line) {return lineNo(line);},
  2561.  
  2562.     setLine: operation(null, function(line, text) {
  2563.       if (isLine(this.view.doc, line))
  2564.         replaceRange(this, text, {line: line, ch: 0}, {line: line, ch: getLine(this.view.doc, line).text.length});
  2565.     }),
  2566.  
  2567.     removeLine: operation(null, function(line) {
  2568.       if (isLine(this.view.doc, line))
  2569.         replaceRange(this, "", {line: line, ch: 0}, clipPos(this.view.doc, {line: line+1, ch: 0}));
  2570.     }),
  2571.  
  2572.     replaceRange: operation(null, function(code, from, to) {
  2573.       var doc = this.view.doc;
  2574.       from = clipPos(doc, from);
  2575.       to = to ? clipPos(doc, to) : from;
  2576.       return replaceRange(this, code, from, to);
  2577.     }),
  2578.  
  2579.     getRange: function(from, to, lineSep) {
  2580.       var doc = this.view.doc;
  2581.       from = clipPos(doc, from); to = clipPos(doc, to);
  2582.       var l1 = from.line, l2 = to.line;
  2583.       if (l1 == l2) return getLine(doc, l1).text.slice(from.ch, to.ch);
  2584.       var code = [getLine(doc, l1).text.slice(from.ch)];
  2585.       doc.iter(l1 + 1, l2, function(line) { code.push(line.text); });
  2586.       code.push(getLine(doc, l2).text.slice(0, to.ch));
  2587.       return code.join(lineSep || "\n");
  2588.     },
  2589.  
  2590.     triggerOnKeyDown: operation(null, onKeyDown),
  2591.  
  2592.     execCommand: function(cmd) {return commands[cmd](this);},
  2593.  
  2594.     // Stuff used by commands, probably not much use to outside code.
  2595.     moveH: operation(null, function(dir, unit) {
  2596.       var sel = this.view.sel, pos = dir < 0 ? sel.from : sel.to;
  2597.       if (sel.shift || posEq(sel.from, sel.to)) pos = findPosH(this, dir, unit, true);
  2598.       setSelectionUser(this, pos, pos, dir);
  2599.     }),
  2600.  
  2601.     deleteH: operation(null, function(dir, unit) {
  2602.       var sel = this.view.sel;
  2603.       if (!posEq(sel.from, sel.to)) replaceRange(this, "", sel.from, sel.to);
  2604.       else replaceRange(this, "", sel.from, findPosH(this, dir, unit, false));
  2605.       this.curOp.userSelChange = true;
  2606.     }),
  2607.  
  2608.     moveV: operation(null, function(dir, unit) {
  2609.       var view = this.view, doc = view.doc, display = this.display;
  2610.       var dist = 0, cur = selHead(view), pos = cursorCoords(this, cur, "div");
  2611.       var x = pos.left, y;
  2612.       if (view.goalColumn != null) x = view.goalColumn;
  2613.       if (unit == "page") {
  2614.         var pageSize = Math.min(display.wrapper.clientHeight, window.innerHeight || document.documentElement.clientHeight);
  2615.         y = pos.top + dir * pageSize;
  2616.       } else if (unit == "line") {
  2617.         y = dir > 0 ? pos.bottom + 3 : pos.top - 3;
  2618.       }
  2619.       do {
  2620.         var target = coordsChar(this, x, y);
  2621.         y += dir * 5;
  2622.       } while (target.outside && (dir < 0 ? y > 0 : y < doc.height));
  2623.  
  2624.       if (unit == "page") display.scrollbarV.scrollTop += charCoords(this, target, "div").top - pos.top;
  2625.       setSelectionUser(this, target, target, dir);
  2626.       view.goalColumn = x;
  2627.     }),
  2628.  
  2629.     toggleOverwrite: function() {
  2630.       if (this.view.overwrite = !this.view.overwrite)
  2631.         this.display.cursor.className += " CodeMirror-overwrite";
  2632.       else
  2633.         this.display.cursor.className = this.display.cursor.className.replace(" CodeMirror-overwrite", "");
  2634.     },
  2635.  
  2636.     posFromIndex: function(off) {
  2637.       var lineNo = 0, ch, doc = this.view.doc;
  2638.       doc.iter(0, doc.size, function(line) {
  2639.         var sz = line.text.length + 1;
  2640.         if (sz > off) { ch = off; return true; }
  2641.         off -= sz;
  2642.         ++lineNo;
  2643.       });
  2644.       return clipPos(doc, {line: lineNo, ch: ch});
  2645.     },
  2646.     indexFromPos: function (coords) {
  2647.       if (coords.line < 0 || coords.ch < 0) return 0;
  2648.       var index = coords.ch;
  2649.       this.view.doc.iter(0, coords.line, function (line) {
  2650.         index += line.text.length + 1;
  2651.       });
  2652.       return index;
  2653.     },
  2654.  
  2655.     scrollTo: function(x, y) {
  2656.       if (x != null) this.display.scrollbarH.scrollLeft = this.display.scroller.scrollLeft = x;
  2657.       if (y != null) this.display.scrollbarV.scrollTop = this.display.scroller.scrollTop = y;
  2658.       updateDisplay(this, []);
  2659.     },
  2660.     getScrollInfo: function() {
  2661.       var scroller = this.display.scroller, co = scrollerCutOff;
  2662.       return {left: scroller.scrollLeft, top: scroller.scrollTop,
  2663.               height: scroller.scrollHeight - co, width: scroller.scrollWidth - co,
  2664.               clientHeight: scroller.clientHeight - co, clientWidth: scroller.clientWidth - co};
  2665.     },
  2666.  
  2667.     scrollIntoView: function(pos) {
  2668.       pos = pos ? clipPos(this.view.doc, pos) : selHead(this.view);
  2669.       var coords = cursorCoords(this, pos);
  2670.       scrollIntoView(this.display, coords.left, coords.top, coords.left, coords.bottom);
  2671.     },
  2672.  
  2673.     setSize: function(width, height) {
  2674.       function interpret(val) {
  2675.         return typeof val == "number" || /^\d+$/.test(String(val)) ? val + "px" : val;
  2676.       }
  2677.       if (width != null) this.display.wrapper.style.width = interpret(width);
  2678.       if (height != null) this.display.wrapper.style.height = interpret(height);
  2679.       this.refresh();
  2680.     },
  2681.  
  2682.     on: function(type, f) {on(this, type, f);},
  2683.     off: function(type, f) {off(this, type, f);},
  2684.  
  2685.     operation: function(f){return operation(this, f)();},
  2686.     compoundChange: function(f){return compoundChange(this, f);},
  2687.  
  2688.     refresh: function(){
  2689.       updateDisplay(this, true, this.view.scrollTop);
  2690.       if (this.display.scrollbarV.scrollHeight > this.view.scrollTop)
  2691.         this.display.scrollbarV.scrollTop = this.view.scrollTop;
  2692.     },
  2693.  
  2694.     getInputField: function(){return this.display.input;},
  2695.     getWrapperElement: function(){return this.display.wrapper;},
  2696.     getScrollerElement: function(){return this.display.scroller;},
  2697.     getGutterElement: function(){return this.display.gutters;}
  2698.   };
  2699.  
  2700.   // OPTION DEFAULTS
  2701.  
  2702.   var optionHandlers = CodeMirror.optionHandlers = {};
  2703.  
  2704.   // The default configuration options.
  2705.   var defaults = CodeMirror.defaults = {};
  2706.  
  2707.   function option(name, deflt, handle, notOnInit) {
  2708.     CodeMirror.defaults[name] = deflt;
  2709.     if (handle) optionHandlers[name] =
  2710.       notOnInit ? function(cm, val, old) {if (old != Init) handle(cm, val, old);} : handle;
  2711.   }
  2712.  
  2713.   var Init = CodeMirror.Init = {toString: function(){return "CodeMirror.Init";}};
  2714.  
  2715.   // These two are, on init, called from the constructor because they
  2716.   // have to be initialized before the editor can start at all.
  2717.   option("value", "", function(cm, val) {cm.setValue(val);}, true);
  2718.   option("mode", null, loadMode, true);
  2719.  
  2720.   option("indentUnit", 2, loadMode, true);
  2721.   option("indentWithTabs", false);
  2722.   option("smartIndent", true);
  2723.   option("tabSize", 4, function(cm) {loadMode(cm); updateDisplay(cm, true);}, true);
  2724.   option("electricChars", true);
  2725.   option("autoClearEmptyLines", false);
  2726.  
  2727.   option("theme", "default", function(cm, val, old) {
  2728.     themeChanged(cm);
  2729.     if (old != Init) guttersChanged(cm);
  2730.   });
  2731.   option("keyMap", "default", keyMapChanged);
  2732.   option("extraKeys", null);
  2733.  
  2734.   option("onKeyEvent", null);
  2735.   option("onDragEvent", null);
  2736.  
  2737.   option("lineWrapping", false, wrappingChanged);
  2738.   option("gutters", [], function(cm) {
  2739.     setGuttersForLineNumbers(cm.options);
  2740.     guttersChanged(cm);
  2741.   }, true);
  2742.   option("lineNumbers", false, function(cm) {
  2743.     setGuttersForLineNumbers(cm.options);
  2744.     guttersChanged(cm);
  2745.   }, true);
  2746.   option("firstLineNumber", 1, guttersChanged, false);
  2747.   option("lineNumberFormatter", function(integer) {return integer;}, guttersChanged, false);
  2748.   
  2749.   option("readOnly", false, function(cm, val) {
  2750.     if (val == "nocursor") {onBlur(cm); cm.display.input.blur();}
  2751.     else if (!val) resetInput(cm, true);
  2752.   });
  2753.   option("dragDrop", true);
  2754.  
  2755.   option("cursorBlinkRate", 530);
  2756.   option("cursorHeight", 1);
  2757.   option("workTime", 100);
  2758.   option("workDelay", 100);
  2759.   option("flattenSpans", true);
  2760.   option("pollInterval", 100);
  2761.   option("undoDepth", 40);
  2762.   option("viewportMargin", 10, function(cm){cm.refresh();}, true);
  2763.  
  2764.   option("tabindex", null, function(cm, val) {
  2765.     cm.display.input.tabIndex = val || "";
  2766.   });
  2767.   option("autofocus", null);
  2768.  
  2769.   // MODE DEFINITION AND QUERYING
  2770.  
  2771.   // Known modes, by name and by MIME
  2772.   var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
  2773.  
  2774.   CodeMirror.defineMode = function(name, mode) {
  2775.     if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name;
  2776.     if (arguments.length > 2) {
  2777.       mode.dependencies = [];
  2778.       for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]);
  2779.     }
  2780.     modes[name] = mode;
  2781.   };
  2782.  
  2783.   CodeMirror.defineMIME = function(mime, spec) {
  2784.     mimeModes[mime] = spec;
  2785.   };
  2786.  
  2787.   CodeMirror.resolveMode = function(spec) {
  2788.     if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
  2789.       spec = mimeModes[spec];
  2790.     else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec))
  2791.       return CodeMirror.resolveMode("application/xml");
  2792.     if (typeof spec == "string") return {name: spec};
  2793.     else return spec || {name: "null"};
  2794.   };
  2795.  
  2796.   CodeMirror.getMode = function(options, spec) {
  2797.     var spec = CodeMirror.resolveMode(spec);
  2798.     var mfactory = modes[spec.name];
  2799.     if (!mfactory) return CodeMirror.getMode(options, "text/plain");
  2800.     var modeObj = mfactory(options, spec);
  2801.     if (modeExtensions.hasOwnProperty(spec.name)) {
  2802.       var exts = modeExtensions[spec.name];
  2803.       for (var prop in exts) if (exts.hasOwnProperty(prop)) modeObj[prop] = exts[prop];
  2804.     }
  2805.     modeObj.name = spec.name;
  2806.     return modeObj;
  2807.   };
  2808.  
  2809.   CodeMirror.defineMode("null", function() {
  2810.     return {token: function(stream) {stream.skipToEnd();}};
  2811.   });
  2812.   CodeMirror.defineMIME("text/plain", "null");
  2813.  
  2814.   var modeExtensions = CodeMirror.modeExtensions = {};
  2815.   CodeMirror.extendMode = function(mode, properties) {
  2816.     var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {});
  2817.     for (var prop in properties) if (properties.hasOwnProperty(prop))
  2818.       exts[prop] = properties[prop];
  2819.   };
  2820.  
  2821.   // EXTENSIONS
  2822.  
  2823.   CodeMirror.defineExtension = function(name, func) {
  2824.     CodeMirror.prototype[name] = func;
  2825.   };
  2826.  
  2827.   CodeMirror.defineOption = option;
  2828.  
  2829.   var initHooks = [];
  2830.   CodeMirror.defineInitHook = function(f) {initHooks.push(f);};
  2831.  
  2832.   // MODE STATE HANDLING
  2833.  
  2834.   // Utility functions for working with state. Exported because modes
  2835.   // sometimes need to do this.
  2836.   function copyState(mode, state) {
  2837.     if (state === true) return state;
  2838.     if (mode.copyState) return mode.copyState(state);
  2839.     var nstate = {};
  2840.     for (var n in state) {
  2841.       var val = state[n];
  2842.       if (val instanceof Array) val = val.concat([]);
  2843.       nstate[n] = val;
  2844.     }
  2845.     return nstate;
  2846.   }
  2847.   CodeMirror.copyState = copyState;
  2848.  
  2849.   function startState(mode, a1, a2) {
  2850.     return mode.startState ? mode.startState(a1, a2) : true;
  2851.   }
  2852.   CodeMirror.startState = startState;
  2853.  
  2854.   CodeMirror.innerMode = function(mode, state) {
  2855.     while (mode.innerMode) {
  2856.       var info = mode.innerMode(state);
  2857.       state = info.state;
  2858.       mode = info.mode;
  2859.     }
  2860.     return info || {mode: mode, state: state};
  2861.   };
  2862.  
  2863.   // STANDARD COMMANDS
  2864.  
  2865.   var commands = CodeMirror.commands = {
  2866.     selectAll: function(cm) {cm.setSelection({line: 0, ch: 0}, {line: cm.lineCount() - 1});},
  2867.     killLine: function(cm) {
  2868.       var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
  2869.       if (!sel && cm.getLine(from.line).length == from.ch)
  2870.         cm.replaceRange("", from, {line: from.line + 1, ch: 0});
  2871.       else cm.replaceRange("", from, sel ? to : {line: from.line});
  2872.     },
  2873.     deleteLine: function(cm) {var l = cm.getCursor().line; cm.replaceRange("", {line: l, ch: 0}, {line: l});},
  2874.     undo: function(cm) {cm.undo();},
  2875.     redo: function(cm) {cm.redo();},
  2876.     goDocStart: function(cm) {cm.setCursor(0, 0, true);},
  2877.     goDocEnd: function(cm) {cm.setSelection({line: cm.lineCount() - 1}, null, true);},
  2878.     goLineStart: function(cm) {
  2879.       cm.setCursor(lineStart(cm, cm.getCursor().line), null, true);
  2880.     },
  2881.     goLineStartSmart: function(cm) {
  2882.       var cur = cm.getCursor(), start = lineStart(cm, cur.line);
  2883.       var line = cm.getLineHandle(start.line);
  2884.       var order = getOrder(line);
  2885.       if (!order || order[0].level == 0) {
  2886.         var firstNonWS = Math.max(0, line.text.search(/\S/));
  2887.         var inWS = cur.line == start.line && cur.ch <= firstNonWS && cur.ch;
  2888.         cm.setCursor(start.line, inWS ? 0 : firstNonWS, true);
  2889.       } else cm.setCursor(start, null, true);
  2890.     },
  2891.     goLineEnd: function(cm) {
  2892.       cm.setCursor(lineEnd(cm, cm.getCursor().line), null, true);
  2893.     },
  2894.     goLineUp: function(cm) {cm.moveV(-1, "line");},
  2895.     goLineDown: function(cm) {cm.moveV(1, "line");},
  2896.     goPageUp: function(cm) {cm.moveV(-1, "page");},
  2897.     goPageDown: function(cm) {cm.moveV(1, "page");},
  2898.     goCharLeft: function(cm) {cm.moveH(-1, "char");},
  2899.     goCharRight: function(cm) {cm.moveH(1, "char");},
  2900.     goColumnLeft: function(cm) {cm.moveH(-1, "column");},
  2901.     goColumnRight: function(cm) {cm.moveH(1, "column");},
  2902.     goWordLeft: function(cm) {cm.moveH(-1, "word");},
  2903.     goWordRight: function(cm) {cm.moveH(1, "word");},
  2904.     delCharBefore: function(cm) {cm.deleteH(-1, "char");},
  2905.     delCharAfter: function(cm) {cm.deleteH(1, "char");},
  2906.     delWordBefore: function(cm) {cm.deleteH(-1, "word");},
  2907.     delWordAfter: function(cm) {cm.deleteH(1, "word");},
  2908.     indentAuto: function(cm) {cm.indentSelection("smart");},
  2909.     indentMore: function(cm) {cm.indentSelection("add");},
  2910.     indentLess: function(cm) {cm.indentSelection("subtract");},
  2911.     insertTab: function(cm) {cm.replaceSelection("\t", "end");},
  2912.     defaultTab: function(cm) {
  2913.       if (cm.somethingSelected()) cm.indentSelection("add");
  2914.       else cm.replaceSelection("\t", "end");
  2915.     },
  2916.     transposeChars: function(cm) {
  2917.       var cur = cm.getCursor(), line = cm.getLine(cur.line);
  2918.       if (cur.ch > 0 && cur.ch < line.length - 1)
  2919.         cm.replaceRange(line.charAt(cur.ch) + line.charAt(cur.ch - 1),
  2920.                         {line: cur.line, ch: cur.ch - 1}, {line: cur.line, ch: cur.ch + 1});
  2921.     },
  2922.     newlineAndIndent: function(cm) {
  2923.       cm.replaceSelection("\n", "end");
  2924.       cm.indentLine(cm.getCursor().line);
  2925.     },
  2926.     toggleOverwrite: function(cm) {cm.toggleOverwrite();}
  2927.   };
  2928.  
  2929.   // STANDARD KEYMAPS
  2930.  
  2931.   var keyMap = CodeMirror.keyMap = {};
  2932.   keyMap.basic = {
  2933.     "Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown",
  2934.     "End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown",
  2935.     "Delete": "delCharAfter", "Backspace": "delCharBefore", "Tab": "defaultTab", "Shift-Tab": "indentAuto",
  2936.     "Enter": "newlineAndIndent", "Insert": "toggleOverwrite"
  2937.   };
  2938.   // Note that the save and find-related commands aren't defined by
  2939.   // default. Unknown commands are simply ignored.
  2940.   keyMap.pcDefault = {
  2941.     "Ctrl-A": "selectAll", "Ctrl-D": "deleteLine", "Ctrl-Z": "undo", "Shift-Ctrl-Z": "redo", "Ctrl-Y": "redo",
  2942.     "Ctrl-Home": "goDocStart", "Alt-Up": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Down": "goDocEnd",
  2943.     "Ctrl-Left": "goWordLeft", "Ctrl-Right": "goWordRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd",
  2944.     "Ctrl-Backspace": "delWordBefore", "Ctrl-Delete": "delWordAfter", "Ctrl-S": "save", "Ctrl-F": "find",
  2945.     "Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll",
  2946.     "Ctrl-[": "indentLess", "Ctrl-]": "indentMore",
  2947.     fallthrough: "basic"
  2948.   };
  2949.   keyMap.macDefault = {
  2950.     "Cmd-A": "selectAll", "Cmd-D": "deleteLine", "Cmd-Z": "undo", "Shift-Cmd-Z": "redo", "Cmd-Y": "redo",
  2951.     "Cmd-Up": "goDocStart", "Cmd-End": "goDocEnd", "Cmd-Down": "goDocEnd", "Alt-Left": "goWordLeft",
  2952.     "Alt-Right": "goWordRight", "Cmd-Left": "goLineStart", "Cmd-Right": "goLineEnd", "Alt-Backspace": "delWordBefore",
  2953.     "Ctrl-Alt-Backspace": "delWordAfter", "Alt-Delete": "delWordAfter", "Cmd-S": "save", "Cmd-F": "find",
  2954.     "Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll",
  2955.     "Cmd-[": "indentLess", "Cmd-]": "indentMore",
  2956.     fallthrough: ["basic", "emacsy"]
  2957.   };
  2958.   keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault;
  2959.   keyMap.emacsy = {
  2960.     "Ctrl-F": "goCharRight", "Ctrl-B": "goCharLeft", "Ctrl-P": "goLineUp", "Ctrl-N": "goLineDown",
  2961.     "Alt-F": "goWordRight", "Alt-B": "goWordLeft", "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd",
  2962.     "Ctrl-V": "goPageUp", "Shift-Ctrl-V": "goPageDown", "Ctrl-D": "delCharAfter", "Ctrl-H": "delCharBefore",
  2963.     "Alt-D": "delWordAfter", "Alt-Backspace": "delWordBefore", "Ctrl-K": "killLine", "Ctrl-T": "transposeChars"
  2964.   };
  2965.  
  2966.   // KEYMAP DISPATCH
  2967.  
  2968.   function getKeyMap(val) {
  2969.     if (typeof val == "string") return keyMap[val];
  2970.     else return val;
  2971.   }
  2972.  
  2973.   function lookupKey(name, extraMap, map, handle, stop) {
  2974.     function lookup(map) {
  2975.       map = getKeyMap(map);
  2976.       var found = map[name];
  2977.       if (found === false) {
  2978.         if (stop) stop();
  2979.         return true;
  2980.       }
  2981.       if (found != null && handle(found)) return true;
  2982.       if (map.nofallthrough) {
  2983.         if (stop) stop();
  2984.         return true;
  2985.       }
  2986.       var fallthrough = map.fallthrough;
  2987.       if (fallthrough == null) return false;
  2988.       if (Object.prototype.toString.call(fallthrough) != "[object Array]")
  2989.         return lookup(fallthrough);
  2990.       for (var i = 0, e = fallthrough.length; i < e; ++i) {
  2991.         if (lookup(fallthrough[i])) return true;
  2992.       }
  2993.       return false;
  2994.     }
  2995.     if (extraMap && lookup(extraMap)) return true;
  2996.     return lookup(map);
  2997.   }
  2998.   function isModifierKey(event) {
  2999.     var name = keyNames[e_prop(event, "keyCode")];
  3000.     return name == "Ctrl" || name == "Alt" || name == "Shift" || name == "Mod";
  3001.   }
  3002.   CodeMirror.isModifierKey = isModifierKey;
  3003.  
  3004.   // FROMTEXTAREA
  3005.  
  3006.   CodeMirror.fromTextArea = function(textarea, options) {
  3007.     if (!options) options = {};
  3008.     options.value = textarea.value;
  3009.     if (!options.tabindex && textarea.tabindex)
  3010.       options.tabindex = textarea.tabindex;
  3011.     // Set autofocus to true if this textarea is focused, or if it has
  3012.     // autofocus and no other element is focused.
  3013.     if (options.autofocus == null) {
  3014.       var hasFocus = document.body;
  3015.       // doc.activeElement occasionally throws on IE
  3016.       try { hasFocus = document.activeElement; } catch(e) {}
  3017.       options.autofocus = hasFocus == textarea ||
  3018.         textarea.getAttribute("autofocus") != null && hasFocus == document.body;
  3019.     }
  3020.  
  3021.     function save() {textarea.value = cm.getValue();}
  3022.     if (textarea.form) {
  3023.       // Deplorable hack to make the submit method do the right thing.
  3024.       on(textarea.form, "submit", save);
  3025.       if (typeof textarea.form.submit == "function") {
  3026.         var realSubmit = textarea.form.submit;
  3027.         textarea.form.submit = function wrappedSubmit() {
  3028.           save();
  3029.           textarea.form.submit = realSubmit;
  3030.           textarea.form.submit();
  3031.           textarea.form.submit = wrappedSubmit;
  3032.         };
  3033.       }
  3034.     }
  3035.  
  3036.     textarea.style.display = "none";
  3037.     var cm = CodeMirror(function(node) {
  3038.       textarea.parentNode.insertBefore(node, textarea.nextSibling);
  3039.     }, options);
  3040.     cm.save = save;
  3041.     cm.getTextArea = function() { return textarea; };
  3042.     cm.toTextArea = function() {
  3043.       save();
  3044.       textarea.parentNode.removeChild(cm.getWrapperElement());
  3045.       textarea.style.display = "";
  3046.       if (textarea.form) {
  3047.         off(textarea.form, "submit", save);
  3048.         if (typeof textarea.form.submit == "function")
  3049.           textarea.form.submit = realSubmit;
  3050.       }
  3051.     };
  3052.     return cm;
  3053.   };
  3054.  
  3055.   // STRING STREAM
  3056.  
  3057.   // Fed to the mode parsers, provides helper functions to make
  3058.   // parsers more succinct.
  3059.  
  3060.   // The character stream used by a mode's parser.
  3061.   function StringStream(string, tabSize) {
  3062.     this.pos = this.start = 0;
  3063.     this.string = string;
  3064.     this.tabSize = tabSize || 8;
  3065.   }
  3066.  
  3067.   StringStream.prototype = {
  3068.     eol: function() {return this.pos >= this.string.length;},
  3069.     sol: function() {return this.pos == 0;},
  3070.     peek: function() {return this.string.charAt(this.pos) || undefined;},
  3071.     next: function() {
  3072.       if (this.pos < this.string.length)
  3073.         return this.string.charAt(this.pos++);
  3074.     },
  3075.     eat: function(match) {
  3076.       var ch = this.string.charAt(this.pos);
  3077.       if (typeof match == "string") var ok = ch == match;
  3078.       else var ok = ch && (match.test ? match.test(ch) : match(ch));
  3079.       if (ok) {++this.pos; return ch;}
  3080.     },
  3081.     eatWhile: function(match) {
  3082.       var start = this.pos;
  3083.       while (this.eat(match)){}
  3084.       return this.pos > start;
  3085.     },
  3086.     eatSpace: function() {
  3087.       var start = this.pos;
  3088.       while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
  3089.       return this.pos > start;
  3090.     },
  3091.     skipToEnd: function() {this.pos = this.string.length;},
  3092.     skipTo: function(ch) {
  3093.       var found = this.string.indexOf(ch, this.pos);
  3094.       if (found > -1) {this.pos = found; return true;}
  3095.     },
  3096.     backUp: function(n) {this.pos -= n;},
  3097.     column: function() {return countColumn(this.string, this.start, this.tabSize);},
  3098.     indentation: function() {return countColumn(this.string, null, this.tabSize);},
  3099.     match: function(pattern, consume, caseInsensitive) {
  3100.       if (typeof pattern == "string") {
  3101.         var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
  3102.         if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
  3103.           if (consume !== false) this.pos += pattern.length;
  3104.           return true;
  3105.         }
  3106.       } else {
  3107.         var match = this.string.slice(this.pos).match(pattern);
  3108.         if (match && match.index > 0) return null;
  3109.         if (match && consume !== false) this.pos += match[0].length;
  3110.         return match;
  3111.       }
  3112.     },
  3113.     current: function(){return this.string.slice(this.start, this.pos);}
  3114.   };
  3115.   CodeMirror.StringStream = StringStream;
  3116.  
  3117.   // TEXTMARKERS
  3118.  
  3119.   function TextMarker(cm, type) {
  3120.     this.lines = [];
  3121.     this.type = type;
  3122.     this.cm = cm;
  3123.   }
  3124.  
  3125.   TextMarker.prototype.clear = function() {
  3126.     if (this.explicitlyCleared) return;
  3127.     startOperation(this.cm);
  3128.     var min = null, max = null;
  3129.     for (var i = 0; i < this.lines.length; ++i) {
  3130.       var line = this.lines[i];
  3131.       var span = getMarkedSpanFor(line.markedSpans, this);
  3132.       if (span.to != null) max = lineNo(line);
  3133.       line.markedSpans = removeMarkedSpan(line.markedSpans, span);
  3134.       if (span.from != null)
  3135.         min = lineNo(line);
  3136.       else if (this.collapsed && !lineIsHidden(line))
  3137.         updateLineHeight(line, textHeight(this.cm.display));
  3138.     }
  3139.     if (min != null) regChange(this.cm, min, max + 1);
  3140.     this.lines.length = 0;
  3141.     this.explicitlyCleared = true;
  3142.     if (this.collapsed && this.cm.view.cantEdit) {
  3143.       this.cm.view.cantEdit = false;
  3144.       reCheckSelection(this.cm);
  3145.     }
  3146.     endOperation(this.cm);
  3147.     signalLater(this.cm, this, "clear");
  3148.   };
  3149.  
  3150.   TextMarker.prototype.find = function() {
  3151.     var from, to;
  3152.     for (var i = 0; i < this.lines.length; ++i) {
  3153.       var line = this.lines[i];
  3154.       var span = getMarkedSpanFor(line.markedSpans, this);
  3155.       if (span.from != null || span.to != null) {
  3156.         var found = lineNo(line);
  3157.         if (span.from != null) from = {line: found, ch: span.from};
  3158.         if (span.to != null) to = {line: found, ch: span.to};
  3159.       }
  3160.     }
  3161.     if (this.type == "bookmark") return from;
  3162.     return from && {from: from, to: to};
  3163.   };
  3164.  
  3165.   function markText(cm, from, to, options, type) {
  3166.     var doc = cm.view.doc;
  3167.     var marker = new TextMarker(cm, type);
  3168.     if (type == "range" && !posLess(from, to)) return marker;
  3169.     if (options) for (var opt in options) if (options.hasOwnProperty(opt))
  3170.       marker[opt] = options[opt];
  3171.     if (marker.replacedWith) {
  3172.       marker.collapsed = true;
  3173.       marker.replacedWith = elt("span", [marker.replacedWith], "CodeMirror-widget");
  3174.     }
  3175.     if (marker.collapsed) sawCollapsedSpans = true;
  3176.  
  3177.     var curLine = from.line, size = 0, collapsedAtStart, collapsedAtEnd;
  3178.     doc.iter(curLine, to.line + 1, function(line) {
  3179.       var span = {from: null, to: null, marker: marker};
  3180.       size += line.text.length;
  3181.       if (curLine == from.line) {span.from = from.ch; size -= from.ch;}
  3182.       if (curLine == to.line) {span.to = to.ch; size -= line.text.length - to.ch;}
  3183.       if (marker.collapsed) {
  3184.         if (curLine == to.line) collapsedAtEnd = collapsedSpanAt(line, to.ch);
  3185.         if (curLine == from.line) collapsedAtStart = collapsedSpanAt(line, from.ch);
  3186.         else updateLineHeight(line, 0);
  3187.       }
  3188.       addMarkedSpan(line, span);
  3189.       if (marker.collapsed && curLine == from.line && lineIsHidden(line))
  3190.         updateLineHeight(line, 0);
  3191.       ++curLine;
  3192.     });
  3193.  
  3194.     if (marker.readOnly) {
  3195.       sawReadOnlySpans = true;
  3196.       if (cm.view.history.done.length || cm.view.history.undone.length)
  3197.         cm.clearHistory();
  3198.     }
  3199.     if (marker.collapsed) {
  3200.       if (collapsedAtStart != collapsedAtEnd)
  3201.         throw new Error("Inserting collapsed marker overlapping an existing one");
  3202.       marker.size = size;
  3203.       marker.atomic = true;
  3204.     }
  3205.     if (marker.className || marker.startStyle || marker.endStyle || marker.collapsed)
  3206.       regChange(cm, from.line, to.line + 1);
  3207.     if (marker.atomic) reCheckSelection(cm);
  3208.     return marker;
  3209.   }
  3210.  
  3211.   // TEXTMARKER SPANS
  3212.  
  3213.   function getMarkedSpanFor(spans, marker) {
  3214.     if (spans) for (var i = 0; i < spans.length; ++i) {
  3215.       var span = spans[i];
  3216.       if (span.marker == marker) return span;
  3217.     }
  3218.   }
  3219.   function removeMarkedSpan(spans, span) {
  3220.     for (var r, i = 0; i < spans.length; ++i)
  3221.       if (spans[i] != span) (r || (r = [])).push(spans[i]);
  3222.     return r;
  3223.   }
  3224.   function addMarkedSpan(line, span) {
  3225.     line.markedSpans = line.markedSpans ? line.markedSpans.concat([span]) : [span];
  3226.     span.marker.lines.push(line);
  3227.   }
  3228.  
  3229.   function markedSpansBefore(old, startCh) {
  3230.     if (old) for (var i = 0, nw; i < old.length; ++i) {
  3231.       var span = old[i], marker = span.marker;
  3232.       var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= startCh : span.from < startCh);
  3233.       if (startsBefore || marker.type == "bookmark" && span.from == startCh) {
  3234.         var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= startCh : span.to > startCh);
  3235.         (nw || (nw = [])).push({from: span.from,
  3236.                                 to: endsAfter ? null : span.to,
  3237.                                 marker: marker});
  3238.       }
  3239.     }
  3240.     return nw;
  3241.   }
  3242.  
  3243.   function markedSpansAfter(old, startCh, endCh) {
  3244.     if (old) for (var i = 0, nw; i < old.length; ++i) {
  3245.       var span = old[i], marker = span.marker;
  3246.       var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= endCh : span.to > endCh);
  3247.       if (endsAfter || marker.type == "bookmark" && span.from == endCh && span.from != startCh) {
  3248.         var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= endCh : span.from < endCh);
  3249.         (nw || (nw = [])).push({from: startsBefore ? null : span.from - endCh,
  3250.                                 to: span.to == null ? null : span.to - endCh,
  3251.                                 marker: marker});
  3252.       }
  3253.     }
  3254.     return nw;
  3255.   }
  3256.  
  3257.   function updateMarkedSpans(oldFirst, oldLast, startCh, endCh, newText) {
  3258.     if (!oldFirst && !oldLast) return newText;
  3259.     // Get the spans that 'stick out' on both sides
  3260.     var first = markedSpansBefore(oldFirst, startCh);
  3261.     var last = markedSpansAfter(oldLast, startCh, endCh);
  3262.  
  3263.     // Next, merge those two ends
  3264.     var sameLine = newText.length == 1, offset = lst(newText).length + (sameLine ? startCh : 0);
  3265.     if (first) {
  3266.       // Fix up .to properties of first
  3267.       for (var i = 0; i < first.length; ++i) {
  3268.         var span = first[i];
  3269.         if (span.to == null) {
  3270.           var found = getMarkedSpanFor(last, span.marker);
  3271.           if (!found) span.to = startCh;
  3272.           else if (sameLine) span.to = found.to == null ? null : found.to + offset;
  3273.         }
  3274.       }
  3275.     }
  3276.     if (last) {
  3277.       // Fix up .from in last (or move them into first in case of sameLine)
  3278.       for (var i = 0; i < last.length; ++i) {
  3279.         var span = last[i];
  3280.         if (span.to != null) span.to += offset;
  3281.         if (span.from == null) {
  3282.           var found = getMarkedSpanFor(first, span.marker);
  3283.           if (!found) {
  3284.             span.from = offset;
  3285.             if (sameLine) (first || (first = [])).push(span);
  3286.           }
  3287.         } else {
  3288.           span.from += offset;
  3289.           if (sameLine) (first || (first = [])).push(span);
  3290.         }
  3291.       }
  3292.     }
  3293.  
  3294.     var newMarkers = [newHL(newText[0], first)];
  3295.     if (!sameLine) {
  3296.       // Fill gap with whole-line-spans
  3297.       var gap = newText.length - 2, gapMarkers;
  3298.       if (gap > 0 && first)
  3299.         for (var i = 0; i < first.length; ++i)
  3300.           if (first[i].to == null)
  3301.             (gapMarkers || (gapMarkers = [])).push({from: null, to: null, marker: first[i].marker});
  3302.       for (var i = 0; i < gap; ++i)
  3303.         newMarkers.push(newHL(newText[i+1], gapMarkers));
  3304.       newMarkers.push(newHL(lst(newText), last));
  3305.     }
  3306.     return newMarkers;
  3307.   }
  3308.  
  3309.   function removeReadOnlyRanges(doc, from, to) {
  3310.     var markers = null;
  3311.     doc.iter(from.line, to.line + 1, function(line) {
  3312.       if (line.markedSpans) for (var i = 0; i < line.markedSpans.length; ++i) {
  3313.         var mark = line.markedSpans[i].marker;
  3314.         if (mark.readOnly && (!markers || indexOf(markers, mark) == -1))
  3315.           (markers || (markers = [])).push(mark);
  3316.       }
  3317.     });
  3318.     if (!markers) return null;
  3319.     var parts = [{from: from, to: to}];
  3320.     for (var i = 0; i < markers.length; ++i) {
  3321.       var m = markers[i].find();
  3322.       for (var j = 0; j < parts.length; ++j) {
  3323.         var p = parts[j];
  3324.         if (!posLess(m.from, p.to) || posLess(m.to, p.from)) continue;
  3325.         var newParts = [j, 1];
  3326.         if (posLess(p.from, m.from)) newParts.push({from: p.from, to: m.from});
  3327.         if (posLess(m.to, p.to)) newParts.push({from: m.to, to: p.to});
  3328.         parts.splice.apply(parts, newParts);
  3329.         j += newParts.length - 1;
  3330.       }
  3331.     }
  3332.     return parts;
  3333.   }
  3334.  
  3335.   function collapsedSpanAt(line, ch) {
  3336.     var sps = sawCollapsedSpans && line.markedSpans, found;
  3337.     if (sps) for (var sp, i = 0; i < sps.length; ++i) {
  3338.       sp = sps[i];
  3339.       if (!sp.marker.collapsed) continue;
  3340.       if ((sp.from == null || sp.from < ch) &&
  3341.           (sp.to == null || sp.to > ch) &&
  3342.           (!found || found.width < sp.marker.width))
  3343.         found = sp.marker;
  3344.     }
  3345.     return found;
  3346.   }
  3347.   function collapsedSpanAtStart(line) { return collapsedSpanAt(line, -1); }
  3348.   function collapsedSpanAtEnd(line) { return collapsedSpanAt(line, line.text.length + 1); }
  3349.  
  3350.   function lineIsHidden(line) {
  3351.     var sps = sawCollapsedSpans && line.markedSpans;
  3352.     if (sps) for (var sp, i = 0; i < sps.length; ++i) {
  3353.       sp = sps[i];
  3354.       if (!sp.marker.collapsed) continue;
  3355.       if (sp.from == null) return true;
  3356.       if (sp.from == 0 && sp.marker.inclusiveLeft && lineIsHiddenInner(line, sp))
  3357.         return true;
  3358.     }
  3359.   }
  3360.   window.lineIsHidden = lineIsHidden;
  3361.   function lineIsHiddenInner(line, span) {
  3362.     if (span.to == null || span.marker.inclusiveRight && span.to == line.text.length)
  3363.       return true;
  3364.     for (var sp, i = 0; i < line.markedSpans.length; ++i) {
  3365.       sp = line.markedSpans[i];
  3366.       if (sp.marker.collapsed && sp.from == span.to &&
  3367.           (sp.marker.inclusiveLeft || span.marker.inclusiveRight) &&
  3368.           lineIsHiddenInner(line, sp)) return true;
  3369.     }
  3370.   }
  3371.  
  3372.   // hl stands for history-line, a data structure that can be either a
  3373.   // string (line without markers) or a {text, markedSpans} object.
  3374.   function hlText(val) { return typeof val == "string" ? val : val.text; }
  3375.   function hlSpans(val) {
  3376.     if (typeof val == "string") return null;
  3377.     var spans = val.markedSpans, out = null;
  3378.     for (var i = 0; i < spans.length; ++i) {
  3379.       if (spans[i].marker.explicitlyCleared) { if (!out) out = spans.slice(0, i); }
  3380.       else if (out) out.push(spans[i]);
  3381.     }
  3382.     return !out ? spans : out.length ? out : null;
  3383.   }
  3384.   function newHL(text, spans) { return spans ? {text: text, markedSpans: spans} : text; }
  3385.  
  3386.   function detachMarkedSpans(line) {
  3387.     var spans = line.markedSpans;
  3388.     if (!spans) return;
  3389.     for (var i = 0; i < spans.length; ++i) {
  3390.       var lines = spans[i].marker.lines;
  3391.       var ix = indexOf(lines, line);
  3392.       lines.splice(ix, 1);
  3393.     }
  3394.     line.markedSpans = null;
  3395.   }
  3396.  
  3397.   function attachMarkedSpans(line, spans) {
  3398.     if (!spans) return;
  3399.     for (var i = 0; i < spans.length; ++i)
  3400.       var marker = spans[i].marker.lines.push(line);
  3401.     line.markedSpans = spans;
  3402.   }
  3403.  
  3404.   // LINE DATA STRUCTURE
  3405.  
  3406.   // Line objects. These hold state related to a line, including
  3407.   // highlighting info (the styles array).
  3408.   function makeLine(text, markedSpans, height) {
  3409.     var line = {text: text, height: height};
  3410.     attachMarkedSpans(line, markedSpans);
  3411.     if (markedSpans && collapsedSpanAtStart(line)) line.height = 0;
  3412.     return line;
  3413.   }
  3414.  
  3415.   function updateLine(cm, line, text, markedSpans) {
  3416.     line.text = text;
  3417.     line.stateAfter = line.styles = null;
  3418.     if (line.order != null) line.order = null;
  3419.     detachMarkedSpans(line);
  3420.     attachMarkedSpans(line, markedSpans);
  3421.     var hidden = collapsedSpanAtStart(line);
  3422.     if (hidden) line.height = 0;
  3423.     else if (!line.height) line.height = textHeight(cm.display);
  3424.     signalLater(cm, line, "change");
  3425.   }
  3426.  
  3427.   function cleanUpLine(line) {
  3428.     line.parent = null;
  3429.     detachMarkedSpans(line);
  3430.   }
  3431.  
  3432.   // Run the given mode's parser over a line, update the styles
  3433.   // array, which contains alternating fragments of text and CSS
  3434.   // classes.
  3435.   function highlightLine(cm, line, state) {
  3436.     var mode = cm.view.mode, flattenSpans = cm.options.flattenSpans;
  3437.     var changed = !line.styles, pos = 0, curText = "", curStyle = null;
  3438.     var stream = new StringStream(line.text, cm.options.tabSize), st = line.styles || (line.styles = []);
  3439.     if (line.text == "" && mode.blankLine) mode.blankLine(state);
  3440.     while (!stream.eol()) {
  3441.       var style = mode.token(stream, state), substr = stream.current();
  3442.       stream.start = stream.pos;
  3443.       if (!flattenSpans || curStyle != style) {
  3444.         if (curText) {
  3445.           changed = changed || pos >= st.length || curText != st[pos] || curStyle != st[pos+1];
  3446.           st[pos++] = curText; st[pos++] = curStyle;
  3447.         }
  3448.         curText = substr; curStyle = style;
  3449.       } else curText = curText + substr;
  3450.       // Give up when line is ridiculously long
  3451.       if (stream.pos > 5000) break;
  3452.     }
  3453.     if (curText) {
  3454.       changed = changed || pos >= st.length || curText != st[pos] || curStyle != st[pos+1];
  3455.       st[pos++] = curText; st[pos++] = curStyle;
  3456.     }
  3457.     if (stream.pos > 5000) { st[pos++] = line.text.slice(stream.pos); st[pos++] = null; }
  3458.     if (pos != st.length) { st.length = pos; changed = true; }
  3459.     return changed;
  3460.   }
  3461.  
  3462.   // Lightweight form of highlight -- proceed over this line and
  3463.   // update state, but don't save a style array.
  3464.   function processLine(cm, line, state) {
  3465.     var mode = cm.view.mode;
  3466.     var stream = new StringStream(line.text, cm.options.tabSize);
  3467.     if (line.text == "" && mode.blankLine) mode.blankLine(state);
  3468.     while (!stream.eol() && stream.pos <= 5000) {
  3469.       mode.token(stream, state);
  3470.       stream.start = stream.pos;
  3471.     }
  3472.   }
  3473.  
  3474.   // Fetch the parser token for a given character. Useful for hacks
  3475.   // that want to inspect the mode state (say, for completion).
  3476.   function getTokenAt(cm, line, state, ch) {
  3477.     var mode = cm.view.mode;
  3478.     var txt = line.text, stream = new StringStream(txt, cm.options.tabSize);
  3479.     while (stream.pos < ch && !stream.eol()) {
  3480.       stream.start = stream.pos;
  3481.       var style = mode.token(stream, state);
  3482.     }
  3483.     return {start: stream.start,
  3484.             end: stream.pos,
  3485.             string: stream.current(),
  3486.             className: style || null,
  3487.             state: state};
  3488.   }
  3489.  
  3490.   var styleToClassCache = {};
  3491.   function styleToClass(style) {
  3492.     if (!style) return null;
  3493.     return styleToClassCache[style] ||
  3494.       (styleToClassCache[style] = "cm-" + style.replace(/ +/g, " cm-"));
  3495.   }
  3496.  
  3497.   function lineContent(cm, realLine, measure) {
  3498.     var merged, line = realLine, lineBefore, sawBefore;
  3499.     while (merged = collapsedSpanAtStart(line)) {
  3500.       line = getLine(cm.view.doc, merged.find().from.line);
  3501.       if (!lineBefore) lineBefore = line;
  3502.     }
  3503.  
  3504.     var builder = {pre: elt("pre"), col: 0, pos: 0, display: !measure,
  3505.                    measure: null, addedOne: false, cm: cm};
  3506.     if (line.textClass) builder.pre.className = line.textClass;
  3507.  
  3508.     do {
  3509.       if (!line.styles)
  3510.         highlightLine(cm, line, line.stateAfter = getStateBefore(cm, lineNo(line)));
  3511.       builder.measure = line == realLine && measure;
  3512.       builder.pos = 0;
  3513.       builder.addToken = builder.measure ? buildTokenMeasure : buildToken;
  3514.       if (measure && sawBefore && line != realLine && !builder.addedOne) {
  3515.         measure[0] = builder.pre.appendChild(elt("span", "\u200b"));
  3516.         builder.addedOne = true;
  3517.       }
  3518.       var next = insertLineContent(line, builder);
  3519.       sawBefore = line == lineBefore;
  3520.       if (next) line = getLine(cm.view.doc, next.to.line);
  3521.     } while (next);
  3522.  
  3523.     if (measure && !builder.addedOne)
  3524.       measure[0] = builder.pre.appendChild(elt("span", "\u200b"));
  3525.     if (!builder.pre.firstChild && !lineIsHidden(realLine))
  3526.       builder.pre.appendChild(document.createTextNode("\u00a0"));
  3527.  
  3528.     return builder.pre;
  3529.   }
  3530.  
  3531.   var tokenSpecialChars = /[\t\u0000-\u0019\u200b\u2028\u2029\uFEFF]/g;
  3532.   function buildToken(builder, text, style, startStyle, endStyle) {
  3533.     if (!text) return;
  3534.     if (!tokenSpecialChars.test(text)) {
  3535.       builder.col += text.length;
  3536.       var content = document.createTextNode(text);
  3537.     } else {
  3538.       var content = document.createDocumentFragment(), pos = 0;
  3539.       while (true) {
  3540.         tokenSpecialChars.lastIndex = pos;
  3541.         var m = tokenSpecialChars.exec(text);
  3542.         var skipped = m ? m.index - pos : text.length - pos;
  3543.         if (skipped) {
  3544.           content.appendChild(document.createTextNode(text.slice(pos, pos + skipped)));
  3545.           builder.col += skipped;
  3546.         }
  3547.         if (!m) break;
  3548.         pos += skipped + 1;
  3549.         if (m[0] == "\t") {
  3550.           var tabSize = builder.cm.options.tabSize, tabWidth = tabSize - builder.col % tabSize;
  3551.           content.appendChild(elt("span", spaceStr(tabWidth), "cm-tab"));
  3552.           builder.col += tabWidth;
  3553.         } else {
  3554.           var token = elt("span", "\u2022", "cm-invalidchar");
  3555.           token.title = "\\u" + m[0].charCodeAt(0).toString(16);
  3556.           content.appendChild(token);
  3557.           builder.col += 1;
  3558.         }
  3559.       }
  3560.     }
  3561.     if (style || startStyle || endStyle || builder.measure) {
  3562.       var fullStyle = style || "";
  3563.       if (startStyle) fullStyle += startStyle;
  3564.       if (endStyle) fullStyle += endStyle;
  3565.       return builder.pre.appendChild(elt("span", [content], fullStyle));
  3566.     }
  3567.     builder.pre.appendChild(content);
  3568.   }
  3569.  
  3570.   function buildTokenMeasure(builder, text, style, startStyle, endStyle) {
  3571.     for (var i = 0; i < text.length; ++i) {
  3572.       if (i && i < text.length - 1 &&
  3573.           builder.cm.options.lineWrapping &&
  3574.           spanAffectsWrapping.test(text.slice(i - 1, i + 1)))
  3575.         builder.pre.appendChild(elt("wbr"));
  3576.       builder.measure[builder.pos++] =
  3577.         buildToken(builder, text.charAt(i), style,
  3578.                    i == 0 && startStyle, i == text.length - 1 && endStyle);
  3579.     }
  3580.     if (text.length) builder.addedOne = true;
  3581.   }
  3582.  
  3583.   function buildCollapsedSpan(builder, size, widget) {
  3584.     if (widget) {
  3585.       if (!builder.display) widget = widget.cloneNode(true);
  3586.       builder.pre.appendChild(widget);
  3587.       if (builder.measure && size) {
  3588.         builder.measure[builder.pos] = widget;
  3589.         builder.addedOne = true;
  3590.       }
  3591.     }
  3592.     builder.pos += size;
  3593.   }
  3594.  
  3595.   // Outputs a number of spans to make up a line, taking highlighting
  3596.   // and marked text into account.
  3597.   function insertLineContent(line, builder) {
  3598.     var st = line.styles, spans = line.markedSpans;
  3599.     if (!spans) {
  3600.       for (var i = 0; i < st.length; i+=2)
  3601.         builder.addToken(builder, st[i], styleToClass(st[i+1]));
  3602.       return;
  3603.     }
  3604.  
  3605.     var allText = line.text, len = allText.length;
  3606.     var pos = 0, i = 0, text = "", style;
  3607.     var nextChange = 0, spanStyle, spanEndStyle, spanStartStyle, collapsed;
  3608.     for (;;) {
  3609.       if (nextChange == pos) { // Update current marker set
  3610.         spanStyle = spanEndStyle = spanStartStyle = "";
  3611.         collapsed = null; nextChange = Infinity;
  3612.         var foundBookmark = null;
  3613.         for (var j = 0; j < spans.length; ++j) {
  3614.           var sp = spans[j], m = sp.marker;
  3615.           if (sp.from <= pos && (sp.to == null || sp.to > pos)) {
  3616.             if (sp.to != null && nextChange > sp.to) { nextChange = sp.to; spanEndStyle = ""; }
  3617.             if (m.className) spanStyle += " " + m.className;
  3618.             if (m.startStyle && sp.from == pos) spanStartStyle += " " + m.startStyle;
  3619.             if (m.endStyle && sp.to == nextChange) spanEndStyle += " " + m.endStyle;
  3620.             if (m.collapsed && (!collapsed || collapsed.marker.width < m.width))
  3621.               collapsed = sp;
  3622.           } else if (sp.from > pos && nextChange > sp.from) {
  3623.             nextChange = sp.from;
  3624.           }
  3625.           if (m.type == "bookmark" && sp.from == pos && m.replacedWith)
  3626.             foundBookmark = m.replacedWith;
  3627.         }
  3628.         if (collapsed && (collapsed.from || 0) == pos) {
  3629.           buildCollapsedSpan(builder, (collapsed.to == null ? len : collapsed.to) - pos,
  3630.                              collapsed.from != null && collapsed.marker.replacedWith);
  3631.           if (collapsed.to == null) return collapsed.marker.find();
  3632.         }
  3633.         if (foundBookmark && !collapsed) buildCollapsedSpan(builder, 0, foundBookmark);
  3634.       }
  3635.       if (pos >= len) break;
  3636.  
  3637.       var upto = Math.min(len, nextChange);
  3638.       while (true) {
  3639.         if (text) {
  3640.           var end = pos + text.length;
  3641.           if (!collapsed) {
  3642.             var tokenText = end > upto ? text.slice(0, upto - pos) : text;
  3643.             builder.addToken(builder, tokenText, style + spanStyle,
  3644.                              spanStartStyle, pos + tokenText.length == nextChange ? spanEndStyle : "");
  3645.           }
  3646.           if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;}
  3647.           pos = end;
  3648.           spanStartStyle = "";
  3649.         }
  3650.         text = st[i++]; style = styleToClass(st[i++]);
  3651.       }
  3652.     }
  3653.   }
  3654.  
  3655.   // DOCUMENT DATA STRUCTURE
  3656.  
  3657.   function LeafChunk(lines) {
  3658.     this.lines = lines;
  3659.     this.parent = null;
  3660.     for (var i = 0, e = lines.length, height = 0; i < e; ++i) {
  3661.       lines[i].parent = this;
  3662.       height += lines[i].height;
  3663.     }
  3664.     this.height = height;
  3665.   }
  3666.  
  3667.   LeafChunk.prototype = {
  3668.     chunkSize: function() { return this.lines.length; },
  3669.     remove: function(at, n, cm) {
  3670.       for (var i = at, e = at + n; i < e; ++i) {
  3671.         var line = this.lines[i];
  3672.         this.height -= line.height;
  3673.         cleanUpLine(line);
  3674.         signalLater(cm, line, "delete");
  3675.       }
  3676.       this.lines.splice(at, n);
  3677.     },
  3678.     collapse: function(lines) {
  3679.       lines.splice.apply(lines, [lines.length, 0].concat(this.lines));
  3680.     },
  3681.     insertHeight: function(at, lines, height) {
  3682.       this.height += height;
  3683.       this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at));
  3684.       for (var i = 0, e = lines.length; i < e; ++i) lines[i].parent = this;
  3685.     },
  3686.     iterN: function(at, n, op) {
  3687.       for (var e = at + n; at < e; ++at)
  3688.         if (op(this.lines[at])) return true;
  3689.     }
  3690.   };
  3691.  
  3692.   function BranchChunk(children) {
  3693.     this.children = children;
  3694.     var size = 0, height = 0;
  3695.     for (var i = 0, e = children.length; i < e; ++i) {
  3696.       var ch = children[i];
  3697.       size += ch.chunkSize(); height += ch.height;
  3698.       ch.parent = this;
  3699.     }
  3700.     this.size = size;
  3701.     this.height = height;
  3702.     this.parent = null;
  3703.   }
  3704.  
  3705.   BranchChunk.prototype = {
  3706.     chunkSize: function() { return this.size; },
  3707.     remove: function(at, n, callbacks) {
  3708.       this.size -= n;
  3709.       for (var i = 0; i < this.children.length; ++i) {
  3710.         var child = this.children[i], sz = child.chunkSize();
  3711.         if (at < sz) {
  3712.           var rm = Math.min(n, sz - at), oldHeight = child.height;
  3713.           child.remove(at, rm, callbacks);
  3714.           this.height -= oldHeight - child.height;
  3715.           if (sz == rm) { this.children.splice(i--, 1); child.parent = null; }
  3716.           if ((n -= rm) == 0) break;
  3717.           at = 0;
  3718.         } else at -= sz;
  3719.       }
  3720.       if (this.size - n < 25) {
  3721.         var lines = [];
  3722.         this.collapse(lines);
  3723.         this.children = [new LeafChunk(lines)];
  3724.         this.children[0].parent = this;
  3725.       }
  3726.     },
  3727.     collapse: function(lines) {
  3728.       for (var i = 0, e = this.children.length; i < e; ++i) this.children[i].collapse(lines);
  3729.     },
  3730.     insert: function(at, lines) {
  3731.       var height = 0;
  3732.       for (var i = 0, e = lines.length; i < e; ++i) height += lines[i].height;
  3733.       this.insertHeight(at, lines, height);
  3734.     },
  3735.     insertHeight: function(at, lines, height) {
  3736.       this.size += lines.length;
  3737.       this.height += height;
  3738.       for (var i = 0, e = this.children.length; i < e; ++i) {
  3739.         var child = this.children[i], sz = child.chunkSize();
  3740.         if (at <= sz) {
  3741.           child.insertHeight(at, lines, height);
  3742.           if (child.lines && child.lines.length > 50) {
  3743.             while (child.lines.length > 50) {
  3744.               var spilled = child.lines.splice(child.lines.length - 25, 25);
  3745.               var newleaf = new LeafChunk(spilled);
  3746.               child.height -= newleaf.height;
  3747.               this.children.splice(i + 1, 0, newleaf);
  3748.               newleaf.parent = this;
  3749.             }
  3750.             this.maybeSpill();
  3751.           }
  3752.           break;
  3753.         }
  3754.         at -= sz;
  3755.       }
  3756.     },
  3757.     maybeSpill: function() {
  3758.       if (this.children.length <= 10) return;
  3759.       var me = this;
  3760.       do {
  3761.         var spilled = me.children.splice(me.children.length - 5, 5);
  3762.         var sibling = new BranchChunk(spilled);
  3763.         if (!me.parent) { // Become the parent node
  3764.           var copy = new BranchChunk(me.children);
  3765.           copy.parent = me;
  3766.           me.children = [copy, sibling];
  3767.           me = copy;
  3768.         } else {
  3769.           me.size -= sibling.size;
  3770.           me.height -= sibling.height;
  3771.           var myIndex = indexOf(me.parent.children, me);
  3772.           me.parent.children.splice(myIndex + 1, 0, sibling);
  3773.         }
  3774.         sibling.parent = me.parent;
  3775.       } while (me.children.length > 10);
  3776.       me.parent.maybeSpill();
  3777.     },
  3778.     iter: function(from, to, op) { this.iterN(from, to - from, op); },
  3779.     iterN: function(at, n, op) {
  3780.       for (var i = 0, e = this.children.length; i < e; ++i) {
  3781.         var child = this.children[i], sz = child.chunkSize();
  3782.         if (at < sz) {
  3783.           var used = Math.min(n, sz - at);
  3784.           if (child.iterN(at, used, op)) return true;
  3785.           if ((n -= used) == 0) break;
  3786.           at = 0;
  3787.         } else at -= sz;
  3788.       }
  3789.     }
  3790.   };
  3791.  
  3792.   // LINE UTILITIES
  3793.  
  3794.   function lineDoc(line) {
  3795.     for (var d = line.parent; d && d.parent; d = d.parent) {}
  3796.     return d;
  3797.   }
  3798.  
  3799.   function getLine(chunk, n) {
  3800.     while (!chunk.lines) {
  3801.       for (var i = 0;; ++i) {
  3802.         var child = chunk.children[i], sz = child.chunkSize();
  3803.         if (n < sz) { chunk = child; break; }
  3804.         n -= sz;
  3805.       }
  3806.     }
  3807.     return chunk.lines[n];
  3808.   }
  3809.  
  3810.   function updateLineHeight(line, height) {
  3811.     var diff = height - line.height;
  3812.     for (var n = line; n; n = n.parent) n.height += diff;
  3813.   }
  3814.  
  3815.   function lineNo(line) {
  3816.     if (line.parent == null) return null;
  3817.     var cur = line.parent, no = indexOf(cur.lines, line);
  3818.     for (var chunk = cur.parent; chunk; cur = chunk, chunk = chunk.parent) {
  3819.       for (var i = 0, e = chunk.children.length; ; ++i) {
  3820.         if (chunk.children[i] == cur) break;
  3821.         no += chunk.children[i].chunkSize();
  3822.       }
  3823.     }
  3824.     return no;
  3825.   }
  3826.  
  3827.   function lineAtHeight(chunk, h) {
  3828.     var n = 0;
  3829.     outer: do {
  3830.       for (var i = 0, e = chunk.children.length; i < e; ++i) {
  3831.         var child = chunk.children[i], ch = child.height;
  3832.         if (h < ch) { chunk = child; continue outer; }
  3833.         h -= ch;
  3834.         n += child.chunkSize();
  3835.       }
  3836.       return n;
  3837.     } while (!chunk.lines);
  3838.     for (var i = 0, e = chunk.lines.length; i < e; ++i) {
  3839.       var line = chunk.lines[i], lh = line.height;
  3840.       if (h < lh) break;
  3841.       h -= lh;
  3842.     }
  3843.     return n + i;
  3844.   }
  3845.  
  3846.   function heightAtLine(cm, lineObj) {
  3847.     var merged;
  3848.     while (merged = collapsedSpanAtStart(lineObj))
  3849.       lineObj = getLine(cm.view.doc, merged.find().from.line);
  3850.  
  3851.     var h = 0, chunk = lineObj.parent;
  3852.     for (var i = 0; i < chunk.lines.length; ++i) {
  3853.       var line = chunk.lines[i];
  3854.       if (line == lineObj) break;
  3855.       else h += line.height;
  3856.     }
  3857.     for (var p = chunk.parent; p; chunk = p, p = chunk.parent) {
  3858.       for (var i = 0; i < p.children.length; ++i) {
  3859.         var cur = p.children[i];
  3860.         if (cur == chunk) break;
  3861.         else h += cur.height;
  3862.       }
  3863.     }
  3864.     return h;
  3865.   }
  3866.  
  3867.   function getOrder(line) {
  3868.     var order = line.order;
  3869.     if (order == null) order = line.order = bidiOrdering(line.text);
  3870.     return order;
  3871.   }
  3872.  
  3873.   // HISTORY
  3874.  
  3875.   // The history object 'chunks' changes that are made close together
  3876.   // and at almost the same time into bigger undoable units.
  3877.   function makeHistory() {
  3878.     return {time: 0, done: [], undone: [],
  3879.             compound: 0, closed: false, dirtyCounter: 0};
  3880.   }
  3881.  
  3882.   function addChange(history, start, added, old) {
  3883.     history.undone.length = 0;
  3884.     var time = +new Date, cur = lst(history.done), last = cur && lst(cur);
  3885.     var dtime = time - history.time;
  3886.     
  3887.     function updateDirty() {
  3888.       if (history.dirtyCounter < 0) {
  3889.           // The user has made a change after undoing past the last clean state. 
  3890.           // We can never get back to a clean state now until markClean() is called.
  3891.           history.dirtyCounter = NaN;
  3892.       }
  3893.       history.dirtyCounter++;
  3894.     }
  3895.  
  3896.     if (cur && !history.closed && history.compound) {
  3897.       updateDirty();
  3898.       cur.push({start: start, added: added, old: old});
  3899.     } else if (dtime > 400 || !last || history.closed ||
  3900.                last.start > start + old.length || last.start + last.added < start) {
  3901.       updateDirty();
  3902.       history.done.push([{start: start, added: added, old: old}]);
  3903.       history.closed = false;
  3904.     } else {
  3905.       var startBefore = Math.max(0, last.start - start),
  3906.       endAfter = Math.max(0, (start + old.length) - (last.start + last.added));
  3907.       for (var i = startBefore; i > 0; --i) last.old.unshift(old[i - 1]);
  3908.       for (var i = endAfter; i > 0; --i) last.old.push(old[old.length - i]);
  3909.       if (startBefore) last.start = start;
  3910.       last.added += added - (old.length - startBefore - endAfter);
  3911.     }
  3912.     history.time = time;
  3913.   }
  3914.  
  3915.   function compoundChange(cm, f) {
  3916.     var hist = cm.view.history;
  3917.     if (!hist.compound++) hist.closed = true;
  3918.     try { return f(); }
  3919.     finally { if (!--hist.compound) hist.closed = true; }
  3920.   }
  3921.  
  3922.   // EVENT OPERATORS
  3923.  
  3924.   function stopMethod() {e_stop(this);}
  3925.   // Ensure an event has a stop method.
  3926.   function addStop(event) {
  3927.     if (!event.stop) event.stop = stopMethod;
  3928.     return event;
  3929.   }
  3930.  
  3931.   function e_preventDefault(e) {
  3932.     if (e.preventDefault) e.preventDefault();
  3933.     else e.returnValue = false;
  3934.   }
  3935.   function e_stopPropagation(e) {
  3936.     if (e.stopPropagation) e.stopPropagation();
  3937.     else e.cancelBubble = true;
  3938.   }
  3939.   function e_stop(e) {e_preventDefault(e); e_stopPropagation(e);}
  3940.   CodeMirror.e_stop = e_stop;
  3941.   CodeMirror.e_preventDefault = e_preventDefault;
  3942.   CodeMirror.e_stopPropagation = e_stopPropagation;
  3943.  
  3944.   function e_target(e) {return e.target || e.srcElement;}
  3945.   function e_button(e) {
  3946.     var b = e.which;
  3947.     if (b == null) {
  3948.       if (e.button & 1) b = 1;
  3949.       else if (e.button & 2) b = 3;
  3950.       else if (e.button & 4) b = 2;
  3951.     }
  3952.     if (mac && e.ctrlKey && b == 1) b = 3;
  3953.     return b;
  3954.   }
  3955.  
  3956.   // Allow 3rd-party code to override event properties by adding an override
  3957.   // object to an event object.
  3958.   function e_prop(e, prop) {
  3959.     var overridden = e.override && e.override.hasOwnProperty(prop);
  3960.     return overridden ? e.override[prop] : e[prop];
  3961.   }
  3962.  
  3963.   // EVENT HANDLING
  3964.  
  3965.   function on(emitter, type, f) {
  3966.     if (emitter.addEventListener)
  3967.       emitter.addEventListener(type, f, false);
  3968.     else if (emitter.attachEvent)
  3969.       emitter.attachEvent("on" + type, f);
  3970.     else {
  3971.       var map = emitter._handlers || (emitter._handlers = {});
  3972.       var arr = map[type] || (map[type] = []);
  3973.       arr.push(f);
  3974.     }
  3975.   }
  3976.  
  3977.   function off(emitter, type, f) {
  3978.     if (emitter.removeEventListener)
  3979.       emitter.removeEventListener(type, f, false);
  3980.     else if (emitter.detachEvent)
  3981.       emitter.detachEvent("on" + type, f);
  3982.     else {
  3983.       var arr = emitter._handlers && emitter._handlers[type];
  3984.       if (!arr) return;
  3985.       for (var i = 0; i < arr.length; ++i)
  3986.         if (arr[i] == f) { arr.splice(i, 1); break; }
  3987.     }
  3988.   }
  3989.  
  3990.   function signal(emitter, type /*, values...*/) {
  3991.     var arr = emitter._handlers && emitter._handlers[type];
  3992.     if (!arr) return;
  3993.     var args = Array.prototype.slice.call(arguments, 2);
  3994.     for (var i = 0; i < arr.length; ++i) arr[i].apply(null, args);
  3995.   }
  3996.  
  3997.   function signalLater(cm, emitter, type /*, values...*/) {
  3998.     var arr = emitter._handlers && emitter._handlers[type];
  3999.     if (!arr) return;
  4000.     var args = Array.prototype.slice.call(arguments, 3), flist = cm.curOp && cm.curOp.delayedCallbacks;
  4001.     function bnd(f) {return function(){f.apply(null, args);};};
  4002.     for (var i = 0; i < arr.length; ++i)
  4003.       if (flist) flist.push(bnd(arr[i]));
  4004.       else arr[i].apply(null, args);
  4005.   }
  4006.  
  4007.   function hasHandler(emitter, type) {
  4008.     var arr = emitter._handlers && emitter._handlers[type];
  4009.     return arr && arr.length > 0;
  4010.   }
  4011.  
  4012.   CodeMirror.on = on; CodeMirror.off = off; CodeMirror.signal = signal;
  4013.  
  4014.   // MISC UTILITIES
  4015.  
  4016.   // Number of pixels added to scroller and sizer to hide scrollbar
  4017.   var scrollerCutOff = 30;
  4018.  
  4019.   // Returned or thrown by various protocols to signal 'I'm not
  4020.   // handling this'.
  4021.   var Pass = CodeMirror.Pass = {toString: function(){return "CodeMirror.Pass";}};
  4022.  
  4023.   function Delayed() {this.id = null;}
  4024.   Delayed.prototype = {set: function(ms, f) {clearTimeout(this.id); this.id = setTimeout(f, ms);}};
  4025.  
  4026.   // Counts the column offset in a string, taking tabs into account.
  4027.   // Used mostly to find indentation.
  4028.   function countColumn(string, end, tabSize) {
  4029.     if (end == null) {
  4030.       end = string.search(/[^\s\u00a0]/);
  4031.       if (end == -1) end = string.length;
  4032.     }
  4033.     for (var i = 0, n = 0; i < end; ++i) {
  4034.       if (string.charAt(i) == "\t") n += tabSize - (n % tabSize);
  4035.       else ++n;
  4036.     }
  4037.     return n;
  4038.   }
  4039.   CodeMirror.countColumn = countColumn;
  4040.  
  4041.   var spaceStrs = [""];
  4042.   function spaceStr(n) {
  4043.     while (spaceStrs.length <= n)
  4044.       spaceStrs.push(lst(spaceStrs) + " ");
  4045.     return spaceStrs[n];
  4046.   }
  4047.  
  4048.   function lst(arr) { return arr[arr.length-1]; }
  4049.  
  4050.   function selectInput(node) {
  4051.     if (ios) { // Mobile Safari apparently has a bug where select() is broken.
  4052.       node.selectionStart = 0;
  4053.       node.selectionEnd = node.value.length;
  4054.     } else node.select();
  4055.   }
  4056.  
  4057.   // Used to position the cursor after an undo/redo by finding the
  4058.   // last edited character.
  4059.   function editEnd(from, to) {
  4060.     if (!to) return 0;
  4061.     if (!from) return to.length;
  4062.     for (var i = from.length, j = to.length; i >= 0 && j >= 0; --i, --j)
  4063.       if (from.charAt(i) != to.charAt(j)) break;
  4064.     return j + 1;
  4065.   }
  4066.  
  4067.   function indexOf(collection, elt) {
  4068.     if (collection.indexOf) return collection.indexOf(elt);
  4069.     for (var i = 0, e = collection.length; i < e; ++i)
  4070.       if (collection[i] == elt) return i;
  4071.     return -1;
  4072.   }
  4073.  
  4074.   function emptyArray(size) {
  4075.     for (var a = [], i = 0; i < size; ++i) a.push(undefined);
  4076.     return a;
  4077.   }
  4078.  
  4079.   function bind(f) {
  4080.     var args = Array.prototype.slice.call(arguments, 1);
  4081.     return function(){return f.apply(null, args);};
  4082.   }
  4083.  
  4084.   var nonASCIISingleCaseWordChar = /[\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc]/;
  4085.   function isWordChar(ch) {
  4086.     return /\w/.test(ch) || ch > "\x80" &&
  4087.       (ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch));
  4088.   }
  4089.  
  4090.   function isEmpty(obj) {
  4091.     var c = 0;
  4092.     for (var n in obj) if (obj.hasOwnProperty(n) && obj[n]) ++c;
  4093.     return !c;
  4094.   }
  4095.  
  4096.   var isExtendingChar = /[\u0300-\u036F\u0483-\u0487\u0488-\u0489\u0591-\u05BD\u05BF\u05C1-\u05C2\u05C4-\u05C5\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7-\u06E8\u06EA-\u06ED\uA66F\uA670-\uA672\uA674-\uA67D\uA69F]/;
  4097.  
  4098.   // DOM UTILITIES
  4099.  
  4100.   function elt(tag, content, className, style) {
  4101.     var e = document.createElement(tag);
  4102.     if (className) e.className = className;
  4103.     if (style) e.style.cssText = style;
  4104.     if (typeof content == "string") setTextContent(e, content);
  4105.     else if (content) for (var i = 0; i < content.length; ++i) e.appendChild(content[i]);
  4106.     return e;
  4107.   }
  4108.  
  4109.   function removeChildren(e) {
  4110.     e.innerHTML = "";
  4111.     return e;
  4112.   }
  4113.  
  4114.   function removeChildrenAndAdd(parent, e) {
  4115.     return removeChildren(parent).appendChild(e);
  4116.   }
  4117.  
  4118.   function setTextContent(e, str) {
  4119.     if (ie_lt9) {
  4120.       e.innerHTML = "";
  4121.       e.appendChild(document.createTextNode(str));
  4122.     } else e.textContent = str;
  4123.   }
  4124.  
  4125.   // FEATURE DETECTION
  4126.  
  4127.   // Detect drag-and-drop
  4128.   var dragAndDrop = function() {
  4129.     // There is *some* kind of drag-and-drop support in IE6-8, but I
  4130.     // couldn't get it to work yet.
  4131.     if (ie_lt9) return false;
  4132.     var div = elt('div');
  4133.     return "draggable" in div || "dragDrop" in div;
  4134.   }();
  4135.  
  4136.   // Feature-detect whether newlines in textareas are converted to \r\n
  4137.   var lineSep = function () {
  4138.     var te = elt("textarea");
  4139.     te.value = "foo\nbar";
  4140.     if (te.value.indexOf("\r") > -1) return "\r\n";
  4141.     return "\n";
  4142.   }();
  4143.  
  4144.   // For a reason I have yet to figure out, some browsers disallow
  4145.   // word wrapping between certain characters *only* if a new inline
  4146.   // element is started between them. This makes it hard to reliably
  4147.   // measure the position of things, since that requires inserting an
  4148.   // extra span. This terribly fragile set of regexps matches the
  4149.   // character combinations that suffer from this phenomenon on the
  4150.   // various browsers.
  4151.   var spanAffectsWrapping = /^$/; // Won't match any two-character string
  4152.   if (gecko) spanAffectsWrapping = /$'/;
  4153.   else if (safari) spanAffectsWrapping = /\-[^ \-?]|\?[^ !'\"\),.\-\/:;\?\]\}]/;
  4154.   else if (chrome) spanAffectsWrapping = /\-[^ \-\.?]|\?[^ \-\.?\]\}:;!'\"\),\/]|[\.!\"#&%\)*+,:;=>\]|\}~][\(\{\[<]|\$'/;
  4155.  
  4156.   var knownScrollbarWidth;
  4157.   function scrollbarWidth(measure) {
  4158.     if (knownScrollbarWidth != null) return knownScrollbarWidth;
  4159.     var test = elt("div", null, null, "width: 50px; height: 50px; overflow-x: scroll");
  4160.     removeChildrenAndAdd(measure, test);
  4161.     if (test.offsetWidth)
  4162.       knownScrollbarWidth = test.offsetHeight - test.clientHeight;
  4163.     return knownScrollbarWidth || 0;
  4164.   }
  4165.  
  4166.   // See if "".split is the broken IE version, if so, provide an
  4167.   // alternative way to split lines.
  4168.   var splitLines = "\n\nb".split(/\n/).length != 3 ? function(string) {
  4169.     var pos = 0, result = [], l = string.length;
  4170.     while (pos <= l) {
  4171.       var nl = string.indexOf("\n", pos);
  4172.       if (nl == -1) nl = string.length;
  4173.       var line = string.slice(pos, string.charAt(nl - 1) == "\r" ? nl - 1 : nl);
  4174.       var rt = line.indexOf("\r");
  4175.       if (rt != -1) {
  4176.         result.push(line.slice(0, rt));
  4177.         pos += rt + 1;
  4178.       } else {
  4179.         result.push(line);
  4180.         pos = nl + 1;
  4181.       }
  4182.     }
  4183.     return result;
  4184.   } : function(string){return string.split(/\r\n?|\n/);};
  4185.   CodeMirror.splitLines = splitLines;
  4186.  
  4187.   var hasSelection = window.getSelection ? function(te) {
  4188.     try { return te.selectionStart != te.selectionEnd; }
  4189.     catch(e) { return false; }
  4190.   } : function(te) {
  4191.     try {var range = te.ownerDocument.selection.createRange();}
  4192.     catch(e) {}
  4193.     if (!range || range.parentElement() != te) return false;
  4194.     return range.compareEndPoints("StartToEnd", range) != 0;
  4195.   };
  4196.  
  4197.   var hasCopyEvent = (function() {
  4198.     var e = elt("div");
  4199.     if ("oncopy" in e) return true;
  4200.     e.setAttribute("oncopy", "return;");
  4201.     return typeof e.oncopy == 'function';
  4202.   })();
  4203.  
  4204.   // KEY NAMING
  4205.  
  4206.   var keyNames = {3: "Enter", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt",
  4207.                   19: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End",
  4208.                   36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert",
  4209.                   46: "Delete", 59: ";", 91: "Mod", 92: "Mod", 93: "Mod", 109: "-", 107: "=", 127: "Delete",
  4210.                   186: ";", 187: "=", 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\",
  4211.                   221: "]", 222: "'", 63276: "PageUp", 63277: "PageDown", 63275: "End", 63273: "Home",
  4212.                   63234: "Left", 63232: "Up", 63235: "Right", 63233: "Down", 63302: "Insert", 63272: "Delete"};
  4213.   CodeMirror.keyNames = keyNames;
  4214.   (function() {
  4215.     // Number keys
  4216.     for (var i = 0; i < 10; i++) keyNames[i + 48] = String(i);
  4217.     // Alphabetic keys
  4218.     for (var i = 65; i <= 90; i++) keyNames[i] = String.fromCharCode(i);
  4219.     // Function keys
  4220.     for (var i = 1; i <= 12; i++) keyNames[i + 111] = keyNames[i + 63235] = "F" + i;
  4221.   })();
  4222.  
  4223.   // BIDI HELPERS
  4224.  
  4225.   function iterateBidiSections(order, from, to, f) {
  4226.     if (!order) return f(from, to, "ltr");
  4227.     for (var i = 0; i < order.length; ++i) {
  4228.       var part = order[i];
  4229.       if (part.from < to && part.to > from)
  4230.         f(Math.max(part.from, from), Math.min(part.to, to), part.level == 1 ? "rtl" : "ltr");
  4231.     }
  4232.   }
  4233.  
  4234.   function bidiLeft(part) { return part.level % 2 ? part.to : part.from; }
  4235.   function bidiRight(part) { return part.level % 2 ? part.from : part.to; }
  4236.  
  4237.   function lineLeft(line) { var order = getOrder(line); return order ? bidiLeft(order[0]) : 0; }
  4238.   function lineRight(line) {
  4239.     var order = getOrder(line);
  4240.     if (!order) return line.text.length;
  4241.     return bidiRight(lst(order));
  4242.   }
  4243.  
  4244.   function lineStart(cm, lineNo) {
  4245.     var merged, line;
  4246.     while (merged = collapsedSpanAtStart(line = getLine(cm.view.doc, lineNo)))
  4247.       lineNo = merged.find().from.line;
  4248.     var order = getOrder(line);
  4249.     var ch = !order ? 0 : order[0].level % 2 ? lineRight(line) : lineLeft(line);
  4250.     return {line: lineNo, ch: ch};
  4251.   }
  4252.   function lineEnd(cm, lineNo) {
  4253.     var merged, line;
  4254.     while (merged = collapsedSpanAtEnd(line = getLine(cm.view.doc, lineNo)))
  4255.       lineNo = merged.find().to.line;
  4256.     var order = getOrder(line);
  4257.     var ch = !order ? line.text.length : order[0].level % 2 ? lineLeft(line) : lineRight(line);
  4258.     return {line: lineNo, ch: ch};
  4259.   }
  4260.  
  4261.   // This is somewhat involved. It is needed in order to move
  4262.   // 'visually' through bi-directional text -- i.e., pressing left
  4263.   // should make the cursor go left, even when in RTL text. The
  4264.   // tricky part is the 'jumps', where RTL and LTR text touch each
  4265.   // other. This often requires the cursor offset to move more than
  4266.   // one unit, in order to visually move one unit.
  4267.   function moveVisually(line, start, dir, byUnit) {
  4268.     var bidi = getOrder(line);
  4269.     if (!bidi) return moveLogically(line, start, dir, byUnit);
  4270.     var moveOneUnit = byUnit ? function(pos, dir) {
  4271.       do pos += dir;
  4272.       while (pos > 0 && isExtendingChar.test(line.text.charAt(pos)));
  4273.       return pos;
  4274.     } : function(pos, dir) { return pos + dir; };
  4275.     var linedir = bidi[0].level;
  4276.     for (var i = 0; i < bidi.length; ++i) {
  4277.       var part = bidi[i], sticky = part.level % 2 == linedir;
  4278.       if ((part.from < start && part.to > start) ||
  4279.           (sticky && (part.from == start || part.to == start))) break;
  4280.     }
  4281.     var target = moveOneUnit(start, part.level % 2 ? -dir : dir);
  4282.  
  4283.     while (target != null) {
  4284.       if (part.level % 2 == linedir) {
  4285.         if (target < part.from || target > part.to) {
  4286.           part = bidi[i += dir];
  4287.           target = part && (dir > 0 == part.level % 2 ? moveOneUnit(part.to, -1) : moveOneUnit(part.from, 1));
  4288.         } else break;
  4289.       } else {
  4290.         if (target == bidiLeft(part)) {
  4291.           part = bidi[--i];
  4292.           target = part && bidiRight(part);
  4293.         } else if (target == bidiRight(part)) {
  4294.           part = bidi[++i];
  4295.           target = part && bidiLeft(part);
  4296.         } else break;
  4297.       }
  4298.     }
  4299.  
  4300.     return target < 0 || target > line.text.length ? null : target;
  4301.   }
  4302.  
  4303.   function moveLogically(line, start, dir, byUnit) {
  4304.     var target = start + dir;
  4305.     if (byUnit) while (target > 0 && isExtendingChar.test(line.text.charAt(target))) target += dir;
  4306.     return target < 0 || target > line.text.length ? null : target;
  4307.   }
  4308.  
  4309.   // Bidirectional ordering algorithm
  4310.   // See http://unicode.org/reports/tr9/tr9-13.html for the algorithm
  4311.   // that this (partially) implements.
  4312.  
  4313.   // One-char codes used for character types:
  4314.   // L (L):   Left-to-Right
  4315.   // R (R):   Right-to-Left
  4316.   // r (AL):  Right-to-Left Arabic
  4317.   // 1 (EN):  European Number
  4318.   // + (ES):  European Number Separator
  4319.   // % (ET):  European Number Terminator
  4320.   // n (AN):  Arabic Number
  4321.   // , (CS):  Common Number Separator
  4322.   // m (NSM): Non-Spacing Mark
  4323.   // b (BN):  Boundary Neutral
  4324.   // s (B):   Paragraph Separator
  4325.   // t (S):   Segment Separator
  4326.   // w (WS):  Whitespace
  4327.   // N (ON):  Other Neutrals
  4328.  
  4329.   // Returns null if characters are ordered as they appear
  4330.   // (left-to-right), or an array of sections ({from, to, level}
  4331.   // objects) in the order in which they occur visually.
  4332.   var bidiOrdering = (function() {
  4333.     // Character types for codepoints 0 to 0xff
  4334.     var lowTypes = "bbbbbbbbbtstwsbbbbbbbbbbbbbbssstwNN%%%NNNNNN,N,N1111111111NNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbbb,N%%%%NNNNLNNNNN%%11NLNNN1LNNNNNLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLL";
  4335.     // Character types for codepoints 0x600 to 0x6ff
  4336.     var arabicTypes = "rrrrrrrrrrrr,rNNmmmmmmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmrrrrrrrnnnnnnnnnn%nnrrrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmmmmmmNmmmmrrrrrrrrrrrrrrrrrr";
  4337.     function charType(code) {
  4338.       var type = "L";
  4339.       if (code <= 0xff) return lowTypes.charAt(code);
  4340.       else if (0x590 <= code && code <= 0x5f4) return "R";
  4341.       else if (0x600 <= code && code <= 0x6ff) return arabicTypes.charAt(code - 0x600);
  4342.       else if (0x700 <= code && code <= 0x8ac) return "r";
  4343.       else return "L";
  4344.     }
  4345.  
  4346.     var bidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/;
  4347.     var isNeutral = /[stwN]/, isStrong = /[LRr]/, countsAsLeft = /[Lb1n]/, countsAsNum = /[1n]/;
  4348.  
  4349.     return function charOrdering(str) {
  4350.       if (!bidiRE.test(str)) return false;
  4351.       var len = str.length, types = [], startType = null;
  4352.       for (var i = 0, type; i < len; ++i) {
  4353.         types.push(type = charType(str.charCodeAt(i)));
  4354.         if (startType == null) {
  4355.           if (type == "L") startType = "L";
  4356.           else if (type == "R" || type == "r") startType = "R";
  4357.         }
  4358.       }
  4359.       if (startType == null) startType = "L";
  4360.  
  4361.       // W1. Examine each non-spacing mark (NSM) in the level run, and
  4362.       // change the type of the NSM to the type of the previous
  4363.       // character. If the NSM is at the start of the level run, it will
  4364.       // get the type of sor.
  4365.       for (var i = 0, prev = startType; i < len; ++i) {
  4366.         var type = types[i];
  4367.         if (type == "m") types[i] = prev;
  4368.         else prev = type;
  4369.       }
  4370.  
  4371.       // W2. Search backwards from each instance of a European number
  4372.       // until the first strong type (R, L, AL, or sor) is found. If an
  4373.       // AL is found, change the type of the European number to Arabic
  4374.       // number.
  4375.       // W3. Change all ALs to R.
  4376.       for (var i = 0, cur = startType; i < len; ++i) {
  4377.         var type = types[i];
  4378.         if (type == "1" && cur == "r") types[i] = "n";
  4379.         else if (isStrong.test(type)) { cur = type; if (type == "r") types[i] = "R"; }
  4380.       }
  4381.  
  4382.       // W4. A single European separator between two European numbers
  4383.       // changes to a European number. A single common separator between
  4384.       // two numbers of the same type changes to that type.
  4385.       for (var i = 1, prev = types[0]; i < len - 1; ++i) {
  4386.         var type = types[i];
  4387.         if (type == "+" && prev == "1" && types[i+1] == "1") types[i] = "1";
  4388.         else if (type == "," && prev == types[i+1] &&
  4389.                  (prev == "1" || prev == "n")) types[i] = prev;
  4390.         prev = type;
  4391.       }
  4392.  
  4393.       // W5. A sequence of European terminators adjacent to European
  4394.       // numbers changes to all European numbers.
  4395.       // W6. Otherwise, separators and terminators change to Other
  4396.       // Neutral.
  4397.       for (var i = 0; i < len; ++i) {
  4398.         var type = types[i];
  4399.         if (type == ",") types[i] = "N";
  4400.         else if (type == "%") {
  4401.           for (var end = i + 1; end < len && types[end] == "%"; ++end) {}
  4402.           var replace = (i && types[i-1] == "!") || (end < len - 1 && types[end] == "1") ? "1" : "N";
  4403.           for (var j = i; j < end; ++j) types[j] = replace;
  4404.           i = end - 1;
  4405.         }
  4406.       }
  4407.  
  4408.       // W7. Search backwards from each instance of a European number
  4409.       // until the first strong type (R, L, or sor) is found. If an L is
  4410.       // found, then change the type of the European number to L.
  4411.       for (var i = 0, cur = startType; i < len; ++i) {
  4412.         var type = types[i];
  4413.         if (cur == "L" && type == "1") types[i] = "L";
  4414.         else if (isStrong.test(type)) cur = type;
  4415.       }
  4416.  
  4417.       // N1. A sequence of neutrals takes the direction of the
  4418.       // surrounding strong text if the text on both sides has the same
  4419.       // direction. European and Arabic numbers act as if they were R in
  4420.       // terms of their influence on neutrals. Start-of-level-run (sor)
  4421.       // and end-of-level-run (eor) are used at level run boundaries.
  4422.       // N2. Any remaining neutrals take the embedding direction.
  4423.       for (var i = 0; i < len; ++i) {
  4424.         if (isNeutral.test(types[i])) {
  4425.           for (var end = i + 1; end < len && isNeutral.test(types[end]); ++end) {}
  4426.           var before = (i ? types[i-1] : startType) == "L";
  4427.           var after = (end < len - 1 ? types[end] : startType) == "L";
  4428.           var replace = before || after ? "L" : "R";
  4429.           for (var j = i; j < end; ++j) types[j] = replace;
  4430.           i = end - 1;
  4431.         }
  4432.       }
  4433.  
  4434.       // Here we depart from the documented algorithm, in order to avoid
  4435.       // building up an actual levels array. Since there are only three
  4436.       // levels (0, 1, 2) in an implementation that doesn't take
  4437.       // explicit embedding into account, we can build up the order on
  4438.       // the fly, without following the level-based algorithm.
  4439.       var order = [], m;
  4440.       for (var i = 0; i < len;) {
  4441.         if (countsAsLeft.test(types[i])) {
  4442.           var start = i;
  4443.           for (++i; i < len && countsAsLeft.test(types[i]); ++i) {}
  4444.           order.push({from: start, to: i, level: 0});
  4445.         } else {
  4446.           var pos = i, at = order.length;
  4447.           for (++i; i < len && types[i] != "L"; ++i) {}
  4448.           for (var j = pos; j < i;) {
  4449.             if (countsAsNum.test(types[j])) {
  4450.               if (pos < j) order.splice(at, 0, {from: pos, to: j, level: 1});
  4451.               var nstart = j;
  4452.               for (++j; j < i && countsAsNum.test(types[j]); ++j) {}
  4453.               order.splice(at, 0, {from: nstart, to: j, level: 2});
  4454.               pos = j;
  4455.             } else ++j;
  4456.           }
  4457.           if (pos < i) order.splice(at, 0, {from: pos, to: i, level: 1});
  4458.         }
  4459.       }
  4460.       if (order[0].level == 1 && (m = str.match(/^\s+/))) {
  4461.         order[0].from = m[0].length;
  4462.         order.unshift({from: 0, to: m[0].length, level: 0});
  4463.       }
  4464.       if (lst(order).level == 1 && (m = str.match(/\s+$/))) {
  4465.         lst(order).to -= m[0].length;
  4466.         order.push({from: len - m[0].length, to: len, level: 0});
  4467.       }
  4468.       if (order[0].level != lst(order).level)
  4469.         order.push({from: len, to: len, level: order[0].level});
  4470.  
  4471.       return order;
  4472.     };
  4473.   })();
  4474.  
  4475.   // THE END
  4476.  
  4477.   CodeMirror.version = "3.0 B2";
  4478.  
  4479.   return CodeMirror;
  4480. })();
  4481. ;
  4482. CodeMirror.defineMode("css", function(config) {
  4483.   var indentUnit = config.indentUnit, type;
  4484.  
  4485.   var keywords = keySet(["above", "absolute", "activeborder", "activecaption", "afar", "after-white-space", "ahead", "alias", "all", "all-scroll",
  4486.     "alternate", "always", "amharic", "amharic-abegede", "antialiased", "appworkspace", "arabic-indic", "armenian", "asterisks",
  4487.     "auto", "avoid", "background", "backwards", "baseline", "below", "bidi-override", "binary", "bengali", "blink",
  4488.     "block", "block-axis", "bold", "bolder", "border", "border-box", "both", "bottom", "break-all", "break-word", "button",
  4489.     "button-bevel", "buttonface", "buttonhighlight", "buttonshadow", "buttontext", "cambodian", "capitalize", "caps-lock-indicator",
  4490.     "caption", "captiontext", "caret", "cell", "center", "checkbox", "circle", "cjk-earthly-branch", "cjk-heavenly-stem", "cjk-ideographic",
  4491.     "clear", "clip", "close-quote", "col-resize", "collapse", "compact", "condensed", "contain", "content", "content-box", "context-menu",
  4492.     "continuous", "copy", "cover", "crop", "cross", "crosshair", "currentcolor", "cursive", "dashed", "decimal", "decimal-leading-zero", "default",
  4493.     "default-button", "destination-atop", "destination-in", "destination-out", "destination-over", "devanagari", "disc", "discard", "document",
  4494.     "dot-dash", "dot-dot-dash", "dotted", "double", "down", "e-resize", "ease", "ease-in", "ease-in-out", "ease-out", "element",
  4495.     "ellipsis", "embed", "end", "ethiopic", "ethiopic-abegede", "ethiopic-abegede-am-et", "ethiopic-abegede-gez",
  4496.     "ethiopic-abegede-ti-er", "ethiopic-abegede-ti-et", "ethiopic-halehame-aa-er", "ethiopic-halehame-aa-et",
  4497.     "ethiopic-halehame-am-et", "ethiopic-halehame-gez", "ethiopic-halehame-om-et", "ethiopic-halehame-sid-et",
  4498.     "ethiopic-halehame-so-et", "ethiopic-halehame-ti-er", "ethiopic-halehame-ti-et", "ethiopic-halehame-tig", "ew-resize", "expanded",
  4499.     "extra-condensed", "extra-expanded", "fantasy", "fast", "fill", "fixed", "flat", "footnotes", "forwards", "from", "geometricPrecision",
  4500.     "georgian", "graytext", "groove", "gujarati", "gurmukhi", "hand", "hangul", "hangul-consonant", "hebrew", "help",
  4501.     "hidden", "hide", "higher", "highlight", "highlighttext", "hiragana", "hiragana-iroha", "horizontal", "hsl", "hsla", "icon", "ignore",
  4502.     "inactiveborder", "inactivecaption", "inactivecaptiontext", "infinite", "infobackground", "infotext", "inherit", "initial", "inline",
  4503.     "inline-axis", "inline-block", "inline-table", "inset", "inside", "intrinsic", "invert", "italic", "justify", "kannada", "katakana",
  4504.     "katakana-iroha", "khmer", "landscape", "lao", "large", "larger", "left", "level", "lighter", "line-through", "linear", "lines",
  4505.     "list-item", "listbox", "listitem", "local", "logical", "loud", "lower", "lower-alpha", "lower-armenian", "lower-greek",
  4506.     "lower-hexadecimal", "lower-latin", "lower-norwegian", "lower-roman", "lowercase", "ltr", "malayalam", "match", "media-controls-background",
  4507.     "media-current-time-display", "media-fullscreen-button", "media-mute-button", "media-play-button", "media-return-to-realtime-button",
  4508.     "media-rewind-button", "media-seek-back-button", "media-seek-forward-button", "media-slider", "media-sliderthumb", "media-time-remaining-display",
  4509.     "media-volume-slider", "media-volume-slider-container", "media-volume-sliderthumb", "medium", "menu", "menulist", "menulist-button",
  4510.     "menulist-text", "menulist-textfield", "menutext", "message-box", "middle", "min-intrinsic", "mix", "mongolian", "monospace", "move", "multiple",
  4511.     "myanmar", "n-resize", "narrower", "navy", "ne-resize", "nesw-resize", "no-close-quote", "no-drop", "no-open-quote", "no-repeat", "none",
  4512.     "normal", "not-allowed", "nowrap", "ns-resize", "nw-resize", "nwse-resize", "oblique", "octal", "open-quote", "optimizeLegibility",
  4513.     "optimizeSpeed", "oriya", "oromo", "outset", "outside", "overlay", "overline", "padding", "padding-box", "painted", "paused",
  4514.     "persian", "plus-darker", "plus-lighter", "pointer", "portrait", "pre", "pre-line", "pre-wrap", "preserve-3d", "progress",
  4515.     "push-button", "radio", "read-only", "read-write", "read-write-plaintext-only", "relative", "repeat", "repeat-x",
  4516.     "repeat-y", "reset", "reverse", "rgb", "rgba", "ridge", "right", "round", "row-resize", "rtl", "run-in", "running", "s-resize", "sans-serif",
  4517.     "scroll", "scrollbar", "se-resize", "searchfield", "searchfield-cancel-button", "searchfield-decoration", "searchfield-results-button",
  4518.     "searchfield-results-decoration", "semi-condensed", "semi-expanded", "separate", "serif", "show", "sidama", "single",
  4519.     "skip-white-space", "slide", "slider-horizontal", "slider-vertical", "sliderthumb-horizontal", "sliderthumb-vertical", "slow",
  4520.     "small", "small-caps", "small-caption", "smaller", "solid", "somali", "source-atop", "source-in", "source-out", "source-over",
  4521.     "space", "square", "square-button", "start", "static", "status-bar", "stretch", "stroke", "sub", "subpixel-antialiased", "super",
  4522.     "sw-resize", "table", "table-caption", "table-cell", "table-column", "table-column-group", "table-footer-group", "table-header-group",
  4523.     "table-row", "table-row-group", "telugu", "text", "text-bottom", "text-top", "textarea", "textfield", "thai", "thick", "thin",
  4524.     "threeddarkshadow", "threedface", "threedhighlight", "threedlightshadow", "threedshadow", "tibetan", "tigre", "tigrinya-er", "tigrinya-er-abegede",
  4525.     "tigrinya-et", "tigrinya-et-abegede", "to", "top", "transparent", "ultra-condensed", "ultra-expanded", "underline", "up", "upper-alpha", "upper-armenian",
  4526.     "upper-greek", "upper-hexadecimal", "upper-latin", "upper-norwegian", "upper-roman", "uppercase", "urdu", "url", "vertical", "vertical-text", "visible",
  4527.     "visibleFill", "visiblePainted", "visibleStroke", "visual", "w-resize", "wait", "wave", "white", "wider", "window", "windowframe", "windowtext",
  4528.     "x-large", "x-small", "xor", "xx-large", "xx-small", "yellow", "-wap-marquee", "-webkit-activelink", "-webkit-auto", "-webkit-baseline-middle",
  4529.     "-webkit-body", "-webkit-box", "-webkit-center", "-webkit-control", "-webkit-focus-ring-color", "-webkit-grab", "-webkit-grabbing",
  4530.     "-webkit-gradient", "-webkit-inline-box", "-webkit-left", "-webkit-link", "-webkit-marquee", "-webkit-mini-control", "-webkit-nowrap", "-webkit-pictograph",
  4531.     "-webkit-right", "-webkit-small-control", "-webkit-text", "-webkit-xxx-large", "-webkit-zoom-in", "-webkit-zoom-out"]);
  4532.  
  4533.   function keySet(array) { var keys = {}; for (var i = 0; i < array.length; ++i) keys[array[i]] = true; return keys; }
  4534.   function ret(style, tp) {type = tp; return style;}
  4535.  
  4536.   function tokenBase(stream, state) {
  4537.     var ch = stream.next();
  4538.     if (ch == "@") {stream.eatWhile(/[\w\\\-]/); return ret("meta", stream.current());}
  4539.     else if (ch == "/" && stream.eat("*")) {
  4540.       state.tokenize = tokenCComment;
  4541.       return tokenCComment(stream, state);
  4542.     }
  4543.     else if (ch == "<" && stream.eat("!")) {
  4544.       state.tokenize = tokenSGMLComment;
  4545.       return tokenSGMLComment(stream, state);
  4546.     }
  4547.     else if (ch == "=") ret(null, "compare");
  4548.     else if ((ch == "~" || ch == "|") && stream.eat("=")) return ret(null, "compare");
  4549.     else if (ch == "\"" || ch == "'") {
  4550.       state.tokenize = tokenString(ch);
  4551.       return state.tokenize(stream, state);
  4552.     }
  4553.     else if (ch == "#") {
  4554.       stream.eatWhile(/[\w\\\-]/);
  4555.       return ret("atom", "hash");
  4556.     }
  4557.     else if (ch == "!") {
  4558.       stream.match(/^\s*\w*/);
  4559.       return ret("keyword", "important");
  4560.     }
  4561.     else if (/\d/.test(ch)) {
  4562.       stream.eatWhile(/[\w.%]/);
  4563.       return ret("number", "unit");
  4564.     }
  4565.     else if (/[,.+>*\/]/.test(ch)) {
  4566.       return ret(null, "select-op");
  4567.     }
  4568.     else if (/[;{}:\[\]]/.test(ch)) {
  4569.       return ret(null, ch);
  4570.     }
  4571.     else {
  4572.       stream.eatWhile(/[\w\\\-]/);
  4573.       return ret("variable", "variable");
  4574.     }
  4575.   }
  4576.  
  4577.   function tokenCComment(stream, state) {
  4578.     var maybeEnd = false, ch;
  4579.     while ((ch = stream.next()) != null) {
  4580.       if (maybeEnd && ch == "/") {
  4581.         state.tokenize = tokenBase;
  4582.         break;
  4583.       }
  4584.       maybeEnd = (ch == "*");
  4585.     }
  4586.     return ret("comment", "comment");
  4587.   }
  4588.  
  4589.   function tokenSGMLComment(stream, state) {
  4590.     var dashes = 0, ch;
  4591.     while ((ch = stream.next()) != null) {
  4592.       if (dashes >= 2 && ch == ">") {
  4593.         state.tokenize = tokenBase;
  4594.         break;
  4595.       }
  4596.       dashes = (ch == "-") ? dashes + 1 : 0;
  4597.     }
  4598.     return ret("comment", "comment");
  4599.   }
  4600.  
  4601.   function tokenString(quote) {
  4602.     return function(stream, state) {
  4603.       var escaped = false, ch;
  4604.       while ((ch = stream.next()) != null) {
  4605.         if (ch == quote && !escaped)
  4606.           break;
  4607.         escaped = !escaped && ch == "\\";
  4608.       }
  4609.       if (!escaped) state.tokenize = tokenBase;
  4610.       return ret("string", "string");
  4611.     };
  4612.   }
  4613.  
  4614.   return {
  4615.     startState: function(base) {
  4616.       return {tokenize: tokenBase,
  4617.               baseIndent: base || 0,
  4618.               stack: []};
  4619.     },
  4620.  
  4621.     token: function(stream, state) {
  4622.       if (stream.eatSpace()) return null;
  4623.       var style = state.tokenize(stream, state);
  4624.  
  4625.       var context = state.stack[state.stack.length-1];
  4626.       if (type == "hash" && context != "rule") style = "string-2";
  4627.       else if (style == "variable") {
  4628.         if (context == "rule") style = keywords[stream.current()] ? "keyword" : "number";
  4629.         else if (!context || context == "@media{") style = "tag";
  4630.       }
  4631.  
  4632.       if (context == "rule" && /^[\{\};]$/.test(type))
  4633.         state.stack.pop();
  4634.       if (type == "{") {
  4635.         if (context == "@media") state.stack[state.stack.length-1] = "@media{";
  4636.         else state.stack.push("{");
  4637.       }
  4638.       else if (type == "}") state.stack.pop();
  4639.       else if (type == "@media") state.stack.push("@media");
  4640.       else if (context == "{" && type != "comment") state.stack.push("rule");
  4641.       return style;
  4642.     },
  4643.  
  4644.     indent: function(state, textAfter) {
  4645.       var n = state.stack.length;
  4646.       if (/^\}/.test(textAfter))
  4647.         n -= state.stack[state.stack.length-1] == "rule" ? 2 : 1;
  4648.       return state.baseIndent + n * indentUnit;
  4649.     },
  4650.  
  4651.     electricChars: "}"
  4652.   };
  4653. });
  4654.  
  4655. CodeMirror.defineMIME("text/css", "css");
  4656. ;
  4657. CodeMirror.defineMode("javascript", function(config, parserConfig) {
  4658.   var indentUnit = config.indentUnit;
  4659.   var jsonMode = parserConfig.json;
  4660.  
  4661.   // Tokenizer
  4662.  
  4663.   var keywords = function(){
  4664.     function kw(type) {return {type: type, style: "keyword"};}
  4665.     var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  4666.     var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  4667.     return {
  4668.       "if": A, "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  4669.       "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
  4670.       "var": kw("var"), "const": kw("var"), "let": kw("var"),
  4671.       "function": kw("function"), "catch": kw("catch"),
  4672.       "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  4673.       "in": operator, "typeof": operator, "instanceof": operator,
  4674.       "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom
  4675.     };
  4676.   }();
  4677.  
  4678.   var isOperatorChar = /[+\-*&%=<>!?|]/;
  4679.  
  4680.   function chain(stream, state, f) {
  4681.     state.tokenize = f;
  4682.     return f(stream, state);
  4683.   }
  4684.  
  4685.   function nextUntilUnescaped(stream, end) {
  4686.     var escaped = false, next;
  4687.     while ((next = stream.next()) != null) {
  4688.       if (next == end && !escaped)
  4689.         return false;
  4690.       escaped = !escaped && next == "\\";
  4691.     }
  4692.     return escaped;
  4693.   }
  4694.  
  4695.   // Used as scratch variables to communicate multiple values without
  4696.   // consing up tons of objects.
  4697.   var type, content;
  4698.   function ret(tp, style, cont) {
  4699.     type = tp; content = cont;
  4700.     return style;
  4701.   }
  4702.  
  4703.   function jsTokenBase(stream, state) {
  4704.     var ch = stream.next();
  4705.     if (ch == '"' || ch == "'")
  4706.       return chain(stream, state, jsTokenString(ch));
  4707.     else if (/[\[\]{}\(\),;\:\.]/.test(ch))
  4708.       return ret(ch);
  4709.     else if (ch == "0" && stream.eat(/x/i)) {
  4710.       stream.eatWhile(/[\da-f]/i);
  4711.       return ret("number", "number");
  4712.     }      
  4713.     else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) {
  4714.       stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
  4715.       return ret("number", "number");
  4716.     }
  4717.     else if (ch == "/") {
  4718.       if (stream.eat("*")) {
  4719.         return chain(stream, state, jsTokenComment);
  4720.       }
  4721.       else if (stream.eat("/")) {
  4722.         stream.skipToEnd();
  4723.         return ret("comment", "comment");
  4724.       }
  4725.       else if (state.reAllowed) {
  4726.         nextUntilUnescaped(stream, "/");
  4727.         stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
  4728.         return ret("regexp", "string-2");
  4729.       }
  4730.       else {
  4731.         stream.eatWhile(isOperatorChar);
  4732.         return ret("operator", null, stream.current());
  4733.       }
  4734.     }
  4735.     else if (ch == "#") {
  4736.         stream.skipToEnd();
  4737.         return ret("error", "error");
  4738.     }
  4739.     else if (isOperatorChar.test(ch)) {
  4740.       stream.eatWhile(isOperatorChar);
  4741.       return ret("operator", null, stream.current());
  4742.     }
  4743.     else {
  4744.       stream.eatWhile(/[\w\$_]/);
  4745.       var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
  4746.       return (known && state.kwAllowed) ? ret(known.type, known.style, word) :
  4747.                      ret("variable", "variable", word);
  4748.     }
  4749.   }
  4750.  
  4751.   function jsTokenString(quote) {
  4752.     return function(stream, state) {
  4753.       if (!nextUntilUnescaped(stream, quote))
  4754.         state.tokenize = jsTokenBase;
  4755.       return ret("string", "string");
  4756.     };
  4757.   }
  4758.  
  4759.   function jsTokenComment(stream, state) {
  4760.     var maybeEnd = false, ch;
  4761.     while (ch = stream.next()) {
  4762.       if (ch == "/" && maybeEnd) {
  4763.         state.tokenize = jsTokenBase;
  4764.         break;
  4765.       }
  4766.       maybeEnd = (ch == "*");
  4767.     }
  4768.     return ret("comment", "comment");
  4769.   }
  4770.  
  4771.   // Parser
  4772.  
  4773.   var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};
  4774.  
  4775.   function JSLexical(indented, column, type, align, prev, info) {
  4776.     this.indented = indented;
  4777.     this.column = column;
  4778.     this.type = type;
  4779.     this.prev = prev;
  4780.     this.info = info;
  4781.     if (align != null) this.align = align;
  4782.   }
  4783.  
  4784.   function inScope(state, varname) {
  4785.     for (var v = state.localVars; v; v = v.next)
  4786.       if (v.name == varname) return true;
  4787.   }
  4788.  
  4789.   function parseJS(state, style, type, content, stream) {
  4790.     var cc = state.cc;
  4791.     // Communicate our context to the combinators.
  4792.     // (Less wasteful than consing up a hundred closures on every call.)
  4793.     cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
  4794.   
  4795.     if (!state.lexical.hasOwnProperty("align"))
  4796.       state.lexical.align = true;
  4797.  
  4798.     while(true) {
  4799.       var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
  4800.       if (combinator(type, content)) {
  4801.         while(cc.length && cc[cc.length - 1].lex)
  4802.           cc.pop()();
  4803.         if (cx.marked) return cx.marked;
  4804.         if (type == "variable" && inScope(state, content)) return "variable-2";
  4805.         return style;
  4806.       }
  4807.     }
  4808.   }
  4809.  
  4810.   // Combinator utils
  4811.  
  4812.   var cx = {state: null, column: null, marked: null, cc: null};
  4813.   function pass() {
  4814.     for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
  4815.   }
  4816.   function cont() {
  4817.     pass.apply(null, arguments);
  4818.     return true;
  4819.   }
  4820.   function register(varname) {
  4821.     var state = cx.state;
  4822.     if (state.context) {
  4823.       cx.marked = "def";
  4824.       for (var v = state.localVars; v; v = v.next)
  4825.         if (v.name == varname) return;
  4826.       state.localVars = {name: varname, next: state.localVars};
  4827.     }
  4828.   }
  4829.  
  4830.   // Combinators
  4831.  
  4832.   var defaultVars = {name: "this", next: {name: "arguments"}};
  4833.   function pushcontext() {
  4834.     if (!cx.state.context) cx.state.localVars = defaultVars;
  4835.     cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
  4836.   }
  4837.   function popcontext() {
  4838.     cx.state.localVars = cx.state.context.vars;
  4839.     cx.state.context = cx.state.context.prev;
  4840.   }
  4841.   function pushlex(type, info) {
  4842.     var result = function() {
  4843.       var state = cx.state;
  4844.       state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical, info);
  4845.     };
  4846.     result.lex = true;
  4847.     return result;
  4848.   }
  4849.   function poplex() {
  4850.     var state = cx.state;
  4851.     if (state.lexical.prev) {
  4852.       if (state.lexical.type == ")")
  4853.         state.indented = state.lexical.indented;
  4854.       state.lexical = state.lexical.prev;
  4855.     }
  4856.   }
  4857.   poplex.lex = true;
  4858.  
  4859.   function expect(wanted) {
  4860.     return function expecting(type) {
  4861.       if (type == wanted) return cont();
  4862.       else if (wanted == ";") return pass();
  4863.       else return cont(arguments.callee);
  4864.     };
  4865.   }
  4866.  
  4867.   function statement(type) {
  4868.     if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
  4869.     if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
  4870.     if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  4871.     if (type == "{") return cont(pushlex("}"), block, poplex);
  4872.     if (type == ";") return cont();
  4873.     if (type == "function") return cont(functiondef);
  4874.     if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
  4875.                                       poplex, statement, poplex);
  4876.     if (type == "variable") return cont(pushlex("stat"), maybelabel);
  4877.     if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  4878.                                          block, poplex, poplex);
  4879.     if (type == "case") return cont(expression, expect(":"));
  4880.     if (type == "default") return cont(expect(":"));
  4881.     if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  4882.                                         statement, poplex, popcontext);
  4883.     return pass(pushlex("stat"), expression, expect(";"), poplex);
  4884.   }
  4885.   function expression(type) {
  4886.     if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);
  4887.     if (type == "function") return cont(functiondef);
  4888.     if (type == "keyword c") return cont(maybeexpression);
  4889.     if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator);
  4890.     if (type == "operator") return cont(expression);
  4891.     if (type == "[") return cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator);
  4892.     if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
  4893.     return cont();
  4894.   }
  4895.   function maybeexpression(type) {
  4896.     if (type.match(/[;\}\)\],]/)) return pass();
  4897.     return pass(expression);
  4898.   }
  4899.     
  4900.   function maybeoperator(type, value) {
  4901.     if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);
  4902.     if (type == "operator" && value == "?") return cont(expression, expect(":"), expression);
  4903.     if (type == ";") return;
  4904.     if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
  4905.     if (type == ".") return cont(property, maybeoperator);
  4906.     if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
  4907.   }
  4908.   function maybelabel(type) {
  4909.     if (type == ":") return cont(poplex, statement);
  4910.     return pass(maybeoperator, expect(";"), poplex);
  4911.   }
  4912.   function property(type) {
  4913.     if (type == "variable") {cx.marked = "property"; return cont();}
  4914.   }
  4915.   function objprop(type) {
  4916.     if (type == "variable") cx.marked = "property";
  4917.     if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);
  4918.   }
  4919.   function commasep(what, end) {
  4920.     function proceed(type) {
  4921.       if (type == ",") return cont(what, proceed);
  4922.       if (type == end) return cont();
  4923.       return cont(expect(end));
  4924.     }
  4925.     return function commaSeparated(type) {
  4926.       if (type == end) return cont();
  4927.       else return pass(what, proceed);
  4928.     };
  4929.   }
  4930.   function block(type) {
  4931.     if (type == "}") return cont();
  4932.     return pass(statement, block);
  4933.   }
  4934.   function vardef1(type, value) {
  4935.     if (type == "variable"){register(value); return cont(vardef2);}
  4936.     return cont();
  4937.   }
  4938.   function vardef2(type, value) {
  4939.     if (value == "=") return cont(expression, vardef2);
  4940.     if (type == ",") return cont(vardef1);
  4941.   }
  4942.   function forspec1(type) {
  4943.     if (type == "var") return cont(vardef1, forspec2);
  4944.     if (type == ";") return pass(forspec2);
  4945.     if (type == "variable") return cont(formaybein);
  4946.     return pass(forspec2);
  4947.   }
  4948.   function formaybein(type, value) {
  4949.     if (value == "in") return cont(expression);
  4950.     return cont(maybeoperator, forspec2);
  4951.   }
  4952.   function forspec2(type, value) {
  4953.     if (type == ";") return cont(forspec3);
  4954.     if (value == "in") return cont(expression);
  4955.     return cont(expression, expect(";"), forspec3);
  4956.   }
  4957.   function forspec3(type) {
  4958.     if (type != ")") cont(expression);
  4959.   }
  4960.   function functiondef(type, value) {
  4961.     if (type == "variable") {register(value); return cont(functiondef);}
  4962.     if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
  4963.   }
  4964.   function funarg(type, value) {
  4965.     if (type == "variable") {register(value); return cont();}
  4966.   }
  4967.  
  4968.   // Interface
  4969.  
  4970.   return {
  4971.     startState: function(basecolumn) {
  4972.       return {
  4973.         tokenize: jsTokenBase,
  4974.         reAllowed: true,
  4975.         kwAllowed: true,
  4976.         cc: [],
  4977.         lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  4978.         localVars: parserConfig.localVars,
  4979.         context: parserConfig.localVars && {vars: parserConfig.localVars},
  4980.         indented: 0
  4981.       };
  4982.     },
  4983.  
  4984.     token: function(stream, state) {
  4985.       if (stream.sol()) {
  4986.         if (!state.lexical.hasOwnProperty("align"))
  4987.           state.lexical.align = false;
  4988.         state.indented = stream.indentation();
  4989.       }
  4990.       if (stream.eatSpace()) return null;
  4991.       var style = state.tokenize(stream, state);
  4992.       if (type == "comment") return style;
  4993.       state.reAllowed = !!(type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/));
  4994.       state.kwAllowed = type != '.';
  4995.       return parseJS(state, style, type, content, stream);
  4996.     },
  4997.  
  4998.     indent: function(state, textAfter) {
  4999.       if (state.tokenize != jsTokenBase) return 0;
  5000.       var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  5001.       if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  5002.       var type = lexical.type, closing = firstChar == type;
  5003.       if (type == "vardef") return lexical.indented + 4;
  5004.       else if (type == "form" && firstChar == "{") return lexical.indented;
  5005.       else if (type == "stat" || type == "form") return lexical.indented + indentUnit;
  5006.       else if (lexical.info == "switch" && !closing)
  5007.         return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  5008.       else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  5009.       else return lexical.indented + (closing ? 0 : indentUnit);
  5010.     },
  5011.  
  5012.     electricChars: ":{}"
  5013.   };
  5014. });
  5015.  
  5016. CodeMirror.defineMIME("text/javascript", "javascript");
  5017. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  5018. ;
  5019. CodeMirror.defineMode("xml", function(config, parserConfig) {
  5020.   var indentUnit = config.indentUnit;
  5021.   var Kludges = parserConfig.htmlMode ? {
  5022.     autoSelfClosers: {'area': true, 'base': true, 'br': true, 'col': true, 'command': true,
  5023.                       'embed': true, 'frame': true, 'hr': true, 'img': true, 'input': true,
  5024.                       'keygen': true, 'link': true, 'meta': true, 'param': true, 'source': true,
  5025.                       'track': true, 'wbr': true},
  5026.     implicitlyClosed: {'dd': true, 'li': true, 'optgroup': true, 'option': true, 'p': true,
  5027.                        'rp': true, 'rt': true, 'tbody': true, 'td': true, 'tfoot': true,
  5028.                        'th': true, 'tr': true},
  5029.     contextGrabbers: {
  5030.       'dd': {'dd': true, 'dt': true},
  5031.       'dt': {'dd': true, 'dt': true},
  5032.       'li': {'li': true},
  5033.       'option': {'option': true, 'optgroup': true},
  5034.       'optgroup': {'optgroup': true},
  5035.       'p': {'address': true, 'article': true, 'aside': true, 'blockquote': true, 'dir': true,
  5036.             'div': true, 'dl': true, 'fieldset': true, 'footer': true, 'form': true,
  5037.             'h1': true, 'h2': true, 'h3': true, 'h4': true, 'h5': true, 'h6': true,
  5038.             'header': true, 'hgroup': true, 'hr': true, 'menu': true, 'nav': true, 'ol': true,
  5039.             'p': true, 'pre': true, 'section': true, 'table': true, 'ul': true},
  5040.       'rp': {'rp': true, 'rt': true},
  5041.       'rt': {'rp': true, 'rt': true},
  5042.       'tbody': {'tbody': true, 'tfoot': true},
  5043.       'td': {'td': true, 'th': true},
  5044.       'tfoot': {'tbody': true},
  5045.       'th': {'td': true, 'th': true},
  5046.       'thead': {'tbody': true, 'tfoot': true},
  5047.       'tr': {'tr': true}
  5048.     },
  5049.     doNotIndent: {"pre": true},
  5050.     allowUnquoted: true,
  5051.     allowMissing: true
  5052.   } : {
  5053.     autoSelfClosers: {},
  5054.     implicitlyClosed: {},
  5055.     contextGrabbers: {},
  5056.     doNotIndent: {},
  5057.     allowUnquoted: false,
  5058.     allowMissing: false
  5059.   };
  5060.   var alignCDATA = parserConfig.alignCDATA;
  5061.  
  5062.   // Return variables for tokenizers
  5063.   var tagName, type;
  5064.  
  5065.   function inText(stream, state) {
  5066.     function chain(parser) {
  5067.       state.tokenize = parser;
  5068.       return parser(stream, state);
  5069.     }
  5070.  
  5071.     var ch = stream.next();
  5072.     if (ch == "<") {
  5073.       if (stream.eat("!")) {
  5074.         if (stream.eat("[")) {
  5075.           if (stream.match("CDATA[")) return chain(inBlock("atom", "]]>"));
  5076.           else return null;
  5077.         }
  5078.         else if (stream.match("--")) return chain(inBlock("comment", "-->"));
  5079.         else if (stream.match("DOCTYPE", true, true)) {
  5080.           stream.eatWhile(/[\w\._\-]/);
  5081.           return chain(doctype(1));
  5082.         }
  5083.         else return null;
  5084.       }
  5085.       else if (stream.eat("?")) {
  5086.         stream.eatWhile(/[\w\._\-]/);
  5087.         state.tokenize = inBlock("meta", "?>");
  5088.         return "meta";
  5089.       }
  5090.       else {
  5091.         type = stream.eat("/") ? "closeTag" : "openTag";
  5092.         stream.eatSpace();
  5093.         tagName = "";
  5094.         var c;
  5095.         while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c;
  5096.         state.tokenize = inTag;
  5097.         return "tag";
  5098.       }
  5099.     }
  5100.     else if (ch == "&") {
  5101.       var ok;
  5102.       if (stream.eat("#")) {
  5103.         if (stream.eat("x")) {
  5104.           ok = stream.eatWhile(/[a-fA-F\d]/) && stream.eat(";");          
  5105.         } else {
  5106.           ok = stream.eatWhile(/[\d]/) && stream.eat(";");
  5107.         }
  5108.       } else {
  5109.         ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";");
  5110.       }
  5111.       return ok ? "atom" : "error";
  5112.     }
  5113.     else {
  5114.       stream.eatWhile(/[^&<]/);
  5115.       return null;
  5116.     }
  5117.   }
  5118.  
  5119.   function inTag(stream, state) {
  5120.     var ch = stream.next();
  5121.     if (ch == ">" || (ch == "/" && stream.eat(">"))) {
  5122.       state.tokenize = inText;
  5123.       type = ch == ">" ? "endTag" : "selfcloseTag";
  5124.       return "tag";
  5125.     }
  5126.     else if (ch == "=") {
  5127.       type = "equals";
  5128.       return null;
  5129.     }
  5130.     else if (/[\'\"]/.test(ch)) {
  5131.       state.tokenize = inAttribute(ch);
  5132.       return state.tokenize(stream, state);
  5133.     }
  5134.     else {
  5135.       stream.eatWhile(/[^\s\u00a0=<>\"\'\/?]/);
  5136.       return "word";
  5137.     }
  5138.   }
  5139.  
  5140.   function inAttribute(quote) {
  5141.     return function(stream, state) {
  5142.       while (!stream.eol()) {
  5143.         if (stream.next() == quote) {
  5144.           state.tokenize = inTag;
  5145.           break;
  5146.         }
  5147.       }
  5148.       return "string";
  5149.     };
  5150.   }
  5151.  
  5152.   function inBlock(style, terminator) {
  5153.     return function(stream, state) {
  5154.       while (!stream.eol()) {
  5155.         if (stream.match(terminator)) {
  5156.           state.tokenize = inText;
  5157.           break;
  5158.         }
  5159.         stream.next();
  5160.       }
  5161.       return style;
  5162.     };
  5163.   }
  5164.   function doctype(depth) {
  5165.     return function(stream, state) {
  5166.       var ch;
  5167.       while ((ch = stream.next()) != null) {
  5168.         if (ch == "<") {
  5169.           state.tokenize = doctype(depth + 1);
  5170.           return state.tokenize(stream, state);
  5171.         } else if (ch == ">") {
  5172.           if (depth == 1) {
  5173.             state.tokenize = inText;
  5174.             break;
  5175.           } else {
  5176.             state.tokenize = doctype(depth - 1);
  5177.             return state.tokenize(stream, state);
  5178.           }
  5179.         }
  5180.       }
  5181.       return "meta";
  5182.     };
  5183.   }
  5184.  
  5185.   var curState, setStyle;
  5186.   function pass() {
  5187.     for (var i = arguments.length - 1; i >= 0; i--) curState.cc.push(arguments[i]);
  5188.   }
  5189.   function cont() {
  5190.     pass.apply(null, arguments);
  5191.     return true;
  5192.   }
  5193.  
  5194.   function pushContext(tagName, startOfLine) {
  5195.     var noIndent = Kludges.doNotIndent.hasOwnProperty(tagName) || (curState.context && curState.context.noIndent);
  5196.     curState.context = {
  5197.       prev: curState.context,
  5198.       tagName: tagName,
  5199.       indent: curState.indented,
  5200.       startOfLine: startOfLine,
  5201.       noIndent: noIndent
  5202.     };
  5203.   }
  5204.   function popContext() {
  5205.     if (curState.context) curState.context = curState.context.prev;
  5206.   }
  5207.  
  5208.   function element(type) {
  5209.     if (type == "openTag") {
  5210.       curState.tagName = tagName;
  5211.       return cont(attributes, endtag(curState.startOfLine));
  5212.     } else if (type == "closeTag") {
  5213.       var err = false;
  5214.       if (curState.context) {
  5215.         if (curState.context.tagName != tagName) {
  5216.           if (Kludges.implicitlyClosed.hasOwnProperty(curState.context.tagName.toLowerCase())) {
  5217.             popContext();
  5218.           }
  5219.           err = !curState.context || curState.context.tagName != tagName;
  5220.         }
  5221.       } else {
  5222.         err = true;
  5223.       }
  5224.       if (err) setStyle = "error";
  5225.       return cont(endclosetag(err));
  5226.     }
  5227.     return cont();
  5228.   }
  5229.   function endtag(startOfLine) {
  5230.     return function(type) {
  5231.       if (type == "selfcloseTag" ||
  5232.           (type == "endTag" && Kludges.autoSelfClosers.hasOwnProperty(curState.tagName.toLowerCase()))) {
  5233.         maybePopContext(curState.tagName.toLowerCase());
  5234.         return cont();
  5235.       }
  5236.       if (type == "endTag") {
  5237.         maybePopContext(curState.tagName.toLowerCase());
  5238.         pushContext(curState.tagName, startOfLine);
  5239.         return cont();
  5240.       }
  5241.       return cont();
  5242.     };
  5243.   }
  5244.   function endclosetag(err) {
  5245.     return function(type) {
  5246.       if (err) setStyle = "error";
  5247.       if (type == "endTag") { popContext(); return cont(); }
  5248.       setStyle = "error";
  5249.       return cont(arguments.callee);
  5250.     };
  5251.   }
  5252.   function maybePopContext(nextTagName) {
  5253.     var parentTagName;
  5254.     while (true) {
  5255.       if (!curState.context) {
  5256.         return;
  5257.       }
  5258.       parentTagName = curState.context.tagName.toLowerCase();
  5259.       if (!Kludges.contextGrabbers.hasOwnProperty(parentTagName) ||
  5260.           !Kludges.contextGrabbers[parentTagName].hasOwnProperty(nextTagName)) {
  5261.         return;
  5262.       }
  5263.       popContext();
  5264.     }
  5265.   }
  5266.  
  5267.   function attributes(type) {
  5268.     if (type == "word") {setStyle = "attribute"; return cont(attribute, attributes);}
  5269.     if (type == "endTag" || type == "selfcloseTag") return pass();
  5270.     setStyle = "error";
  5271.     return cont(attributes);
  5272.   }
  5273.   function attribute(type) {
  5274.     if (type == "equals") return cont(attvalue, attributes);
  5275.     if (!Kludges.allowMissing) setStyle = "error";
  5276.     return (type == "endTag" || type == "selfcloseTag") ? pass() : cont();
  5277.   }
  5278.   function attvalue(type) {
  5279.     if (type == "string") return cont(attvaluemaybe);
  5280.     if (type == "word" && Kludges.allowUnquoted) {setStyle = "string"; return cont();}
  5281.     setStyle = "error";
  5282.     return (type == "endTag" || type == "selfCloseTag") ? pass() : cont();
  5283.   }
  5284.   function attvaluemaybe(type) {
  5285.     if (type == "string") return cont(attvaluemaybe);
  5286.     else return pass();
  5287.   }
  5288.  
  5289.   return {
  5290.     startState: function() {
  5291.       return {tokenize: inText, cc: [], indented: 0, startOfLine: true, tagName: null, context: null};
  5292.     },
  5293.  
  5294.     token: function(stream, state) {
  5295.       if (stream.sol()) {
  5296.         state.startOfLine = true;
  5297.         state.indented = stream.indentation();
  5298.       }
  5299.       if (stream.eatSpace()) return null;
  5300.  
  5301.       setStyle = type = tagName = null;
  5302.       var style = state.tokenize(stream, state);
  5303.       state.type = type;
  5304.       if ((style || type) && style != "comment") {
  5305.         curState = state;
  5306.         while (true) {
  5307.           var comb = state.cc.pop() || element;
  5308.           if (comb(type || style)) break;
  5309.         }
  5310.       }
  5311.       state.startOfLine = false;
  5312.       return setStyle || style;
  5313.     },
  5314.  
  5315.     indent: function(state, textAfter, fullLine) {
  5316.       var context = state.context;
  5317.       if ((state.tokenize != inTag && state.tokenize != inText) ||
  5318.           context && context.noIndent)
  5319.         return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0;
  5320.       if (alignCDATA && /<!\[CDATA\[/.test(textAfter)) return 0;
  5321.       if (context && /^<\//.test(textAfter))
  5322.         context = context.prev;
  5323.       while (context && !context.startOfLine)
  5324.         context = context.prev;
  5325.       if (context) return context.indent + indentUnit;
  5326.       else return 0;
  5327.     },
  5328.  
  5329.     compareStates: function(a, b) {
  5330.       if (a.indented != b.indented || a.tokenize != b.tokenize) return false;
  5331.       for (var ca = a.context, cb = b.context; ; ca = ca.prev, cb = cb.prev) {
  5332.         if (!ca || !cb) return ca == cb;
  5333.         if (ca.tagName != cb.tagName || ca.indent != cb.indent) return false;
  5334.       }
  5335.     },
  5336.  
  5337.     electricChars: "/"
  5338.   };
  5339. });
  5340.  
  5341. CodeMirror.defineMIME("text/xml", "xml");
  5342. CodeMirror.defineMIME("application/xml", "xml");
  5343. if (!CodeMirror.mimeModes.hasOwnProperty("text/html"))
  5344.   CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true});
  5345. ;
  5346. CodeMirror.defineMode("htmlmixed", function(config, parserConfig) {
  5347.   var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
  5348.   var jsMode = CodeMirror.getMode(config, "javascript");
  5349.   var cssMode = CodeMirror.getMode(config, "css");
  5350.  
  5351.   function html(stream, state) {
  5352.     var style = htmlMode.token(stream, state.htmlState);
  5353.     if (style == "tag" && stream.current() == ">" && state.htmlState.context) {
  5354.       if (/^script$/i.test(state.htmlState.context.tagName)) {
  5355.         state.token = javascript;
  5356.         state.localState = jsMode.startState(htmlMode.indent(state.htmlState, ""));
  5357.         state.mode = "javascript";
  5358.       }
  5359.       else if (/^style$/i.test(state.htmlState.context.tagName)) {
  5360.         state.token = css;
  5361.         state.localState = cssMode.startState(htmlMode.indent(state.htmlState, ""));
  5362.         state.mode = "css";
  5363.       }
  5364.     }
  5365.     return style;
  5366.   }
  5367.   function maybeBackup(stream, pat, style) {
  5368.     var cur = stream.current();
  5369.     var close = cur.search(pat);
  5370.     if (close > -1) stream.backUp(cur.length - close);
  5371.     return style;
  5372.   }
  5373.   function javascript(stream, state) {
  5374.     if (stream.match(/^<\/\s*script\s*>/i, false)) {
  5375.       state.token = html;
  5376.       state.localState = null;
  5377.       state.mode = "html";
  5378.       return html(stream, state);
  5379.     }
  5380.     return maybeBackup(stream, /<\/\s*script\s*>/,
  5381.                        jsMode.token(stream, state.localState));
  5382.   }
  5383.   function css(stream, state) {
  5384.     if (stream.match(/^<\/\s*style\s*>/i, false)) {
  5385.       state.token = html;
  5386.       state.localState = null;
  5387.       state.mode = "html";
  5388.       return html(stream, state);
  5389.     }
  5390.     return maybeBackup(stream, /<\/\s*style\s*>/,
  5391.                        cssMode.token(stream, state.localState));
  5392.   }
  5393.  
  5394.   return {
  5395.     startState: function() {
  5396.       var state = htmlMode.startState();
  5397.       return {token: html, localState: null, mode: "html", htmlState: state};
  5398.     },
  5399.  
  5400.     copyState: function(state) {
  5401.       if (state.localState)
  5402.         var local = CodeMirror.copyState(state.token == css ? cssMode : jsMode, state.localState);
  5403.       return {token: state.token, localState: local, mode: state.mode,
  5404.               htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
  5405.     },
  5406.  
  5407.     token: function(stream, state) {
  5408.       return state.token(stream, state);
  5409.     },
  5410.  
  5411.     indent: function(state, textAfter) {
  5412.       if (state.token == html || /^\s*<\//.test(textAfter))
  5413.         return htmlMode.indent(state.htmlState, textAfter);
  5414.       else if (state.token == javascript)
  5415.         return jsMode.indent(state.localState, textAfter);
  5416.       else
  5417.         return cssMode.indent(state.localState, textAfter);
  5418.     },
  5419.  
  5420.     compareStates: function(a, b) {
  5421.       if (a.mode != b.mode) return false;
  5422.       if (a.localState) return CodeMirror.Pass;
  5423.       return htmlMode.compareStates(a.htmlState, b.htmlState);
  5424.     },
  5425.  
  5426.     electricChars: "/{}:"
  5427.   };
  5428. }, "xml", "javascript", "css");
  5429.  
  5430. CodeMirror.defineMIME("text/html", "htmlmixed");
  5431. ;
  5432.  
  5433. /**
  5434.  * @constructor
  5435.  * @extends {WebInspector.View}
  5436.  * @implements {WebInspector.TextEditor}
  5437.  * @param {?string} url
  5438.  * @param {WebInspector.TextEditorDelegate} delegate
  5439.  */
  5440. WebInspector.CodeMirrorTextEditor = function(url, delegate)
  5441. {
  5442.     WebInspector.View.call(this);
  5443.     this._delegate = delegate;
  5444.     this._url = url;
  5445.  
  5446.     this.registerRequiredCSS("codemirror.css");
  5447.     this.registerRequiredCSS("cmdevtools.css");
  5448.  
  5449.     this._codeMirror = window.CodeMirror(this.element, {
  5450.         lineNumbers: true,
  5451.         gutters: ["CodeMirror-linenumbers", "breakpoints"]
  5452.     });
  5453.  
  5454.     this._codeMirror.on("change", this._change.bind(this));
  5455.     this._codeMirror.on("gutterClick", this._gutterClick.bind(this));
  5456.     this.element.addEventListener("contextmenu", this._contextMenu.bind(this));
  5457.  
  5458.     this._lastRange = this.range();
  5459.  
  5460.     this.element.firstChild.addStyleClass("source-code");
  5461.     this.element.firstChild.addStyleClass("fill");
  5462.     this._elementToWidget = new Map();
  5463. }
  5464.  
  5465. WebInspector.CodeMirrorTextEditor.prototype = {
  5466.     /**
  5467.      * @param {string} mimeType
  5468.      */
  5469.     set mimeType(mimeType)
  5470.     {
  5471.         this._codeMirror.setOption("mode", mimeType);
  5472.         switch(mimeType) {
  5473.             case "text/html": this._codeMirror.setOption("theme", "web-inspector-html"); break;
  5474.             case "text/css": this._codeMirror.setOption("theme", "web-inspector-css"); break;
  5475.             case "text/javascript": this._codeMirror.setOption("theme", "web-inspector-js"); break;
  5476.         }
  5477.     },
  5478.  
  5479.     /**
  5480.      * @param {boolean} readOnly
  5481.      */
  5482.     setReadOnly: function(readOnly)
  5483.     {
  5484.         this._codeMirror.setOption("readOnly", readOnly);
  5485.     },
  5486.  
  5487.     /**
  5488.      * @return {boolean}
  5489.      */
  5490.     readOnly: function()
  5491.     {
  5492.         return !!this._codeMirror.getOption("readOnly");
  5493.     },
  5494.  
  5495.     /**
  5496.      * @return {Element}
  5497.      */
  5498.     defaultFocusedElement: function()
  5499.     {
  5500.         return this.element.firstChild;
  5501.     },
  5502.  
  5503.     focus: function()
  5504.     {
  5505.         this._codeMirror.focus();
  5506.     },
  5507.  
  5508.     beginUpdates: function() { },
  5509.  
  5510.     endUpdates: function() { },
  5511.  
  5512.     /**
  5513.      * @param {number} lineNumber
  5514.      */
  5515.     revealLine: function(lineNumber)
  5516.     {
  5517.         this._codeMirror.setCursor({ line: lineNumber, ch: 0 });
  5518.         this._codeMirror.scrollIntoView();
  5519.     },
  5520.  
  5521.     _gutterClick: function(instance, lineNumber, gutter, event)
  5522.     {
  5523.         this.dispatchEventToListeners(WebInspector.TextEditor.Events.GutterClick, { lineNumber: lineNumber, event: event });
  5524.     },
  5525.  
  5526.     _contextMenu: function(event)
  5527.     {
  5528.         var contextMenu = new WebInspector.ContextMenu(event);
  5529.         var target = event.target.enclosingNodeOrSelfWithClass("CodeMirror-gutter-elt");
  5530.         if (target)
  5531.             this._delegate.populateLineGutterContextMenu(contextMenu, parseInt(target.textContent, 10) - 1);
  5532.         else
  5533.             this._delegate.populateTextAreaContextMenu(contextMenu, null);
  5534.         contextMenu.show();
  5535.     },
  5536.  
  5537.     /**
  5538.      * @param {number} lineNumber
  5539.      * @param {boolean} disabled
  5540.      * @param {boolean} conditional
  5541.      */
  5542.     addBreakpoint: function(lineNumber, disabled, conditional)
  5543.     {
  5544.         var element = document.createElement("span");
  5545.         element.textContent = lineNumber + 1;
  5546.         element.className = "cm-breakpoint" + (disabled ? " cm-breakpoint-disabled" : "") + (conditional ? " cm-breakpoint-conditional" : "");
  5547.         this._codeMirror.setGutterMarker(lineNumber, "breakpoints", element);
  5548.     },
  5549.  
  5550.     /**
  5551.      * @param {number} lineNumber
  5552.      */
  5553.     removeBreakpoint: function(lineNumber)
  5554.     {
  5555.         this._codeMirror.setGutterMarker(lineNumber, "breakpoints", null);
  5556.     },
  5557.  
  5558.     /**
  5559.      * @param {number} lineNumber
  5560.      */
  5561.     setExecutionLine: function(lineNumber)
  5562.     {
  5563.         this._executionLine = this._codeMirror.getLineHandle(lineNumber);
  5564.         this._codeMirror.addLineClass(this._executionLine, null, "cm-execution-line");
  5565.     },
  5566.  
  5567.     clearExecutionLine: function()
  5568.     {
  5569.         if (this._executionLine)
  5570.             this._codeMirror.removeLineClass(this._executionLine, null, "cm-execution-line");
  5571.         delete this._executionLine;
  5572.     },
  5573.  
  5574.     /**
  5575.      * @param {number} lineNumber
  5576.      * @param {Element} element
  5577.      */
  5578.     addDecoration: function(lineNumber, element)
  5579.     {
  5580.         var widget = this._codeMirror.addLineWidget(lineNumber, element);
  5581.         this._elementToWidget.put(element, widget);
  5582.     },
  5583.  
  5584.     /**
  5585.      * @param {number} lineNumber
  5586.      * @param {Element} element
  5587.      */
  5588.     removeDecoration: function(lineNumber, element)
  5589.     {
  5590.         var widget = this._elementToWidget.remove(element);
  5591.         if (widget)
  5592.             this._codeMirror.removeLineWidget(widget);
  5593.     },
  5594.  
  5595.     /**
  5596.      * @param {WebInspector.TextRange} range
  5597.      */
  5598.     markAndRevealRange: function(range)
  5599.     {
  5600.         if (range)
  5601.             this.setSelection(range);
  5602.     },
  5603.  
  5604.     /**
  5605.      * @param {number} lineNumber
  5606.      */
  5607.     highlightLine: function(lineNumber)
  5608.     {
  5609.         this.clearLineHighlight();
  5610.         this._highlightedLine = this._codeMirror.getLineHandle(lineNumber);
  5611.         if (!this._highlightedLine)
  5612.           return;
  5613.         this.revealLine(lineNumber);
  5614.         this._codeMirror.addLineClass(this._highlightedLine, null, "cm-highlight");
  5615.         this._clearHighlightTimeout = setTimeout(this.clearLineHighlight.bind(this), 2000);
  5616.     },
  5617.  
  5618.     clearLineHighlight: function()
  5619.     {
  5620.         if (this._clearHighlightTimeout)
  5621.             clearTimeout(this._clearHighlightTimeout);
  5622.         delete this._clearHighlightTimeout;
  5623.  
  5624.          if (this._highlightedLine)
  5625.             this._codeMirror.removeLineClass(this._highlightedLine, null, "cm-highlight");
  5626.         delete this._highlightedLine;
  5627.     },
  5628.  
  5629.     /**
  5630.      * @return {Array.<Element>}
  5631.      */
  5632.     elementsToRestoreScrollPositionsFor: function()
  5633.     {
  5634.         return [];
  5635.     },
  5636.  
  5637.     /**
  5638.      * @param {WebInspector.TextEditor} textEditor
  5639.      */
  5640.     inheritScrollPositions: function(textEditor)
  5641.     {
  5642.     },
  5643.  
  5644.     onResize: function()
  5645.     {
  5646.         this._codeMirror.refresh();
  5647.     },
  5648.  
  5649.     /**
  5650.      * @param {WebInspector.TextRange} range
  5651.      * @param {string} text
  5652.      * @return {WebInspector.TextRange}
  5653.      */
  5654.     editRange: function(range, text)
  5655.     {
  5656.         var pos = this._toPos(range);
  5657.         this._codeMirror.replaceRange(text, pos.start, pos.end);
  5658.         var newRange = this._toRange(pos.start, this._codeMirror.posFromIndex(this._codeMirror.indexFromPos(pos.start) + text.length));
  5659.         this._delegate.onTextChanged(range, newRange);
  5660.         return newRange;
  5661.     },
  5662.  
  5663.     _change: function()
  5664.     {
  5665.         var widgets = this._elementToWidget.values();
  5666.         for (var i = 0; i < widgets.length; ++i)
  5667.             this._codeMirror.removeLineWidget(widgets[i]);
  5668.         this._elementToWidget.clear();
  5669.  
  5670.         var newRange = this.range();
  5671.         this._delegate.onTextChanged(this._lastRange, newRange);
  5672.         this._lastRange = newRange;
  5673.     },
  5674.  
  5675.     /**
  5676.      * @param {number} lineNumber
  5677.      */
  5678.     scrollToLine: function(lineNumber)
  5679.     {
  5680.         this._codeMirror.setCursor({line:lineNumber, ch:0});
  5681.     },
  5682.  
  5683.     /**
  5684.      * @return {WebInspector.TextRange}
  5685.      */
  5686.     selection: function(textRange)
  5687.     {
  5688.         var start = this._codeMirror.getCursor(true);
  5689.         var end = this._codeMirror.getCursor(false);
  5690.  
  5691.         if (start.line > end.line || (start.line == end.line && start.ch > end.ch))
  5692.             return this._toRange(end, start);
  5693.  
  5694.         return this._toRange(start, end);
  5695.     },
  5696.  
  5697.     /**
  5698.      * @return {WebInspector.TextRange?}
  5699.      */
  5700.     lastSelection: function()
  5701.     {
  5702.         return this._lastSelection;
  5703.     },
  5704.  
  5705.     /**
  5706.      * @param {WebInspector.TextRange} textRange
  5707.      */
  5708.     setSelection: function(textRange)
  5709.     {
  5710.         this._lastSelection = textRange;
  5711.         var pos = this._toPos(textRange);
  5712.         this._codeMirror.setSelection(pos.start, pos.end);
  5713.     },
  5714.  
  5715.     /**
  5716.      * @param {string} text
  5717.      */
  5718.     setText: function(text)
  5719.     {
  5720.         this._codeMirror.setValue(text);
  5721.     },
  5722.  
  5723.     /**
  5724.      * @return {string}
  5725.      */
  5726.     text: function()
  5727.     {
  5728.         return this._codeMirror.getValue();
  5729.     },
  5730.  
  5731.     /**
  5732.      * @return {WebInspector.TextRange}
  5733.      */
  5734.     range: function()
  5735.     {
  5736.         var lineCount = this.linesCount;
  5737.         var lastLine = this._codeMirror.getLine(lineCount - 1);
  5738.         return this._toRange({ line: 0, ch: 0 }, { line: lineCount - 1, ch: lastLine.length });
  5739.     },
  5740.  
  5741.     /**
  5742.      * @param {number} lineNumber
  5743.      * @return {string}
  5744.      */
  5745.     line: function(lineNumber)
  5746.     {
  5747.         return this._codeMirror.getLine(lineNumber);
  5748.     },
  5749.  
  5750.     /**
  5751.      * @return {number}
  5752.      */
  5753.     get linesCount()
  5754.     {
  5755.         return this._codeMirror.lineCount();
  5756.     },
  5757.  
  5758.     /**
  5759.      * @param {number} line
  5760.      * @param {string} name
  5761.      * @param {Object?} value
  5762.      */
  5763.     setAttribute: function(line, name, value)
  5764.     {
  5765.         var handle = this._codeMirror.getLineHandle(line);
  5766.         if (handle.attributes === undefined) handle.attributes = {};
  5767.         handle.attributes[name] = value;
  5768.     },
  5769.  
  5770.     /**
  5771.      * @param {number} line
  5772.      * @param {string} name
  5773.      * @return {Object|null} value
  5774.      */
  5775.     getAttribute: function(line, name)
  5776.     {
  5777.         var handle = this._codeMirror.getLineHandle(line);
  5778.         return handle.attributes && handle.attributes[name] !== undefined ? handle.attributes[name] : null;
  5779.     },
  5780.  
  5781.     /**
  5782.      * @param {number} line
  5783.      * @param {string} name
  5784.      */
  5785.     removeAttribute: function(line, name)
  5786.     {
  5787.         var handle = this._codeMirror.getLineHandle(line);
  5788.         if (handle && handle.attributes)
  5789.             delete handle.attributes[name];
  5790.     },
  5791.  
  5792.     _toPos: function(range)
  5793.     {
  5794.         return {
  5795.             start: {line: range.startLine, ch: range.startColumn},
  5796.             end: {line: range.endLine, ch: range.endColumn}
  5797.         }
  5798.     },
  5799.  
  5800.     _toRange: function(start, end)
  5801.     {
  5802.         return new WebInspector.TextRange(start.line, start.ch, end.line, end.ch);
  5803.     },
  5804.  
  5805.     __proto__: WebInspector.View.prototype
  5806. }
  5807.